6
$printArr = recursive($newArray); //calls recursive function
$data = [];
var_dump($data);
var_dump($printArr);
 function recursive($array, $level = 0)
 {
 $searchingValue = 'tableName';
 foreach($array as $key => $value)
 {
 //If $value is an array.
 if(is_array($value))
 {
 recursive($value, $level + 1);
 } 
 else
 {
 //It is not an array, so print it out.
 if($key == $searchingValue)
 {
 echo "[".$key . "] => " . $value, '<br>';
 $data[] = $value;
 }
 }
 }
 }

So I have this function and I am trying to save $value value into $data[] array. But it always returns it empty and I don't know why I can't get $value saved outside the function. If i echo $value I get what i need but like I've mentioned the variables doesn't get saved in this case - table names.

Cœur
38.9k25 gold badges206 silver badges281 bronze badges
asked Jul 20, 2018 at 11:45
7
  • 1
    Have you called recursive() function anywhere? Or just declared and printed $data array which is already blank. Commented Jul 20, 2018 at 11:48
  • What purpose does $level serve? Commented Jul 20, 2018 at 11:51
  • Can you explain the goal of the function which you have written? Commented Jul 20, 2018 at 11:51
  • Or even show some sample input and both actual vs. expected output Commented Jul 20, 2018 at 11:56
  • Yes I did. $printArr = recursive($newArray); //calls recursive function $data = []; var_dump($data); var_dump($printArr); I've also added in the question code. Commented Jul 20, 2018 at 11:59

2 Answers 2

6

You need to pass the $data to your recursive function. Also you need to return the $data.

Try this code :

function recursive($array, $level = 0, $data =[])
{
 $searchingValue = 'tableName';
 foreach($array as $key => $value)
 {
 //If $value is an array.
 if(is_array($value))
 {
 recursive($value, $level + 1 , $data);
 } 
 else
 {
 //It is not an array, so print it out.
 if($key == $searchingValue)
 {
 echo "[".$key . "] => " . $value, '<br>';
 $data[] = $value;
 }
 }
 }
 return $data;
}
answered Jul 20, 2018 at 12:05
2
  • If you pass by reference, you dont need to return data, choose one. Also, you are not passing data by reference in example. Commented Jul 20, 2018 at 12:06
  • It worked! I can't believe I didn't think to pass reference. Thank you so much! Commented Jul 20, 2018 at 12:16
0

You can't access variable $data, which is outside the function, from the function. You need to pass it by reference or return it. Small example

<?php
$a = 1;
// Your case
function b() {
 $a = 4;
 return true;
}
// Passing by reference
function c(&$d) {
 $d = 5;
 return true;
}
// Using return
function d($d) {
 $d = 6;
 return $d;
}
b();
var_dump($a);
c($a);
var_dump($a);
$a = d($a);
var_dump($a);

https://3v4l.org/UXFdR

answered Jul 20, 2018 at 12:00

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.