test - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.



Your result can be seen below.

Result of php executing





Full code of test.php

  1. <?php
  2. /*
  3. function search($element, $position, $options = null) {
  4.     $res = null;
  5.  
  6.     if (!empty($element)) {
  7.         $node_name = array_shift($position);
  8.         $next_element = null;
  9.         
  10.         if (!empty($node_name)) {
  11.             if ($node_name === "_base_") {
  12.                 $next_element = $array;
  13.                 
  14.             } else if (array_key_exists($node_name, $element) ) {
  15.                 $next_element = $element[$node_name];
  16.  
  17.             }
  18.             
  19.             var_dump($next_element);
  20.             
  21.             if (!empty($position)) {
  22.                 if (gettype($next_element) === "array") {
  23.                     $res = search($next_element, $position, $options);
  24.                 }
  25.             } else {
  26.                 $res = $next_element;
  27.                 
  28.                 if (!empty($options)) {
  29.                     if (array_key_exists('return', $options)) {
  30.                         $return_type = $options['return'];
  31.  
  32.                         if ($return_type === 'key') {
  33.                             $res = array_key_first($element);
  34.                             
  35.                         } else if($return_type === "value") {
  36.                             $res = $element[array_key_first($element)];
  37.                         }
  38.                     }
  39.                 }
  40.             }
  41.             
  42.             
  43.         }
  44.     }
  45.  
  46.  
  47.     return $res;
  48. }*/
  49.  
  50. function pickValue($array, $paths, $options = null) {
  51.     $path_last_index = count($paths) -1;
  52.     $pointeur = $array;
  53.     
  54.     
  55.     foreach($paths as $index => $path) {
  56.         
  57.        if($index === $path_last_index){
  58.            var_dump($pointeur);
  59.            
  60.        }
  61.         
  62.        $pointeur = $pointeur[$path];
  63.        
  64.     }
  65.     
  66.     //prendre clé ou valeur si options != empty
  67.         
  68.     var_dump($pointeur);
  69.     
  70. }
  71.  
  72. function search($array, $position, $options = null) {
  73.     $res = null;
  74.  
  75.     if (!empty($array)) {
  76.         $node_name = array_shift($position);
  77.         $next_array = null;
  78.         
  79.         if (!empty($node_name)) {
  80.             if ($node_name === "_base_") {
  81.                 $next_array = $array;
  82.                 
  83.             } else if (array_key_exists($node_name, $array) ) {
  84.                 $next_array = $array[$node_name];
  85.  
  86.             }
  87.             
  88.             var_dump($next_array);
  89.             
  90.             if (!empty($position)) {
  91.                 if (gettype($next_array) === "array") {
  92.                     $res = search($next_array, $position, $options);
  93.                 }
  94.             } else {
  95.                 $res = $next_array;
  96.                 
  97.                 if (!empty($options)) {
  98.                     if (array_key_exists('return', $options)) {
  99.                         $return_type = $options['return'];
  100.  
  101.                         if ($return_type === 'key') {
  102.                             $res = array_key_first($array);
  103.                             
  104.                         } else if($return_type === "value") {
  105.                             $res = $array[array_key_first($array)];
  106.                         }
  107.                     }
  108.                 }
  109.             }
  110.             
  111.             
  112.         }
  113.     }
  114.  
  115.  
  116.     return $res;
  117. }
  118.  
  119. $arr = ["LibelleCommercial" => "Santé actif", "Commissionnement" => "3010A", "TarifsCouverture" => [
  120.     "TarifCouverture" => [
  121.         "CodeGarantie" => "MaladieChirurgie",
  122.         "CodeNiveau" => "05",
  123.         "Tarif" => "102.94"
  124.         ]
  125.     ]
  126. ];
  127.  
  128. $arr_1 = [
  129.         'V1' => (float) 38.24
  130. ];
  131.  
  132. //print_r(pickValue($arr, ['TarifsCouverture', "TarifCouverture", "CodeNiveau"], ['return' => 'value']));
  133. print_r(pickValue($arr_1, [], ['return' => 'value']));
File Description
  • test
  • PHP Code
  • 15 Jan-2021
  • 3.51 Kb
You can Share it: