user1483042 user1483042 - 7 months ago 10
PHP Question

PHP Why is this returning a multidimensional array?

Here's my code:

foreach ($states as $staterow) {
$state_set[] = array ('id' =>$staterow['id'],
'st' =>$staterow['st'],
'state' =>$staterow['state']);
}

foreach ($showresult as $display) {

$display_result[] = array ( 'id' => $display['id'],
'name' => $display['name'],
'club_email' => $display['club_email'],
'description' => $display['description'],
'city' => $display['city'],
'state' => $display['state'],
'url' => $display['url'],
'facebook' => $display['facebook']);
unset($dispay_result);

$statearray[] = array ( 'state' => $display['state'] );
}

foreach ($statearray as $display) {
$letterprocess[] = substr($display['state'], 0, 1);
}


foreach ($fulllist as $x => $x_value) {

foreach ($letterprocess as $y => $y_value) {

IF ($x_value == $y_value) {

$true[] = array ($x_value);
}
}
}

var_dump($true);


Why is $true coming out as a multidimensional array? Why would it not come out as a simple array. If I echo $x_value inside the loop it is a variable, not an array.

Here's the var_dump:

array(12) {
[0]=> array(1) {
[0]=> string(1) "A"
}
[1]=> array(1) {
[0]=> string(1) "C"
}
[2]=> array(1) {
[0]=> string(1) "C"
}
[3]=> array(1) {
[0]=> string(1) "I"
}
[4]=> array(1) {
[0]=> string(1) "I"
}
[5]=> array(1) {
[0]=> string(1) "M"
}
[6]=> array(1) {
[0]=> string(1) "M"
}
[7]=> array(1) {
[0]=> string(1) "M"
}
[8]=> array(1) {
[0]=> string(1) "M"
}
[9]=> array(1) {
[0]=> string(1) "M"
}
[10]=> array(1) {
[0]=> string(1) "N"
}
[11]=> array(1) {
[0]=> string(1) "V"
}
}

Answer
$true[] = $x_value;

is what you're after.

In using

$true[] = array ($x_value);

you're explicitly casting $x_value into an array (array ($x_value)), and then adding that array to your $true array to give yourself an array of arrays

Comments