0

I'm programming in php for years, but i have encountered a redicilous problem and have no idea why it has happened. I guess i'm missing something, but my brain has stopped working! I have an ass. array and when i var_dump() it, it's like this:

 array 0 => array 4 => string '12' (length=2) 1 => array 2 => string '10' (length=2) 2 => array 1 => string '9' (length=1) 

I want to do something with those values like storing them in an array, (12, 10, 9), but I dont know how to retrieve it! I have tested foreach(), array_value(), but no result. No matter what i do, the var_dump() result is still the same! by the way i'm using codeigniter framework, but logically it should have nothing to do with the framework thanks guys

4
  • 2
    You seem to have array inside array: so urarray[0]['4'] will return 12, urarray[1]['2'] return 10 and urarray[2]['1'] return 9 Commented Oct 4, 2012 at 21:15
  • Can you show us your attempts with foreach ? Commented Oct 4, 2012 at 21:16
  • 1
    You're programming for years, but you can't tell that this isn't an associative array? Commented Oct 4, 2012 at 21:31
  • 1
    @PeeHaa I'm suspecting PHP as in Partial Hospitalisation Program, it would go some way to explain the reasoning behind the question. In which case likely many years. Commented Oct 4, 2012 at 21:39

5 Answers 5

1

You can try using array_map

$array = array( 0 => array(4 => '12'), 1 => array(2 => '10'), 2 => array(1 => '9')); $array = array_map("array_shift", $array); var_dump($array); 

Output

array 0 => string '12' (length=2) 1 => string '10' (length=2) 2 => string '9' (length=1) 
Sign up to request clarification or add additional context in comments.

1 Comment

fwiw, you can pass 'array_shift' directly to array_map; you don't need to use the anonymous function in the middle
0

You can access them like this:

$array[0][4]= '13'; $array[1][2]= '11'; $array[2][1]= '10'; 

var_dump($array); gives this result:

 array(3) { [0]=> array(1) { [4]=> string(2) "13" } [1]=> array(1) { [2]=> string(2) "11" } [2]=> array(1) { [1]=> string(2) "10" } } 

2 Comments

Not very useful if he doesn't know in advance what the keys are.
I think there is an assumption here that he does know what it looks like, though.
0

like this:

for ($i = count($array) ; $i >= 0 ; $i--) { foreach($array[$1] as $k => $v) { echo $k . "=>".$v; } } 

Comments

0

If you want them to end up in an array, declare one, then iterate over your items.

$arr = new array(); foreach ($arrItem in $yourArray) { foreach ($innerArrItem in $arrItem) { array_push($arr, $innerArrItem); } } print_r($arr); 

Comments

0

If you have an unknown depth, you can do something like this:

$output = array(); array_walk_recursive($input, function($value, $index, &$output) { $output[] = $value; }, $output); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.