6

This is probably a simple question for you php whizzes out there but I can't seem to find an answer in google!

I have a multi-dimensional array which first set of keys are named and I want to change them into numbers like 0, 1, 2..

If it was a normal array I could set $newArray = array_values($multiArr); and it would get rid of the keys and make them numeric! But since its multidimensional theres another set of keys/values underneath this.

Could I somehow use a loop to loop through it and define each one? But then how would I specify the current key?

Any advice would help thank you!

If this helps at all the data coming in is a JSON received from a device and there's something wrong with the encoding so the data looks like this:

`Array
(
    [�w� ��߯19�] => Array
        (
            [down] => 1279146141431
            [up] => 1279146351453
        )
`

So I need to somehow get access to the data underneath each crazy key.

4 Answers 4

28

This code:

$arr = array(
  'a' => array('a' => '1', 'b' => '2', 'c' => '3'),
  'b' => array('d' => '4', 'e' => '5', 'f' => '6'),
  'c' => array('g' => '7', 'h' => '8', 'i' => '9'),
);
$arr2 = array_values($arr);

yields $arr2 in this form:

[0] => Array
    (
        [a] => 1
        [b] => 2
        [c] => 3
    )

[1] => Array
    (
        [d] => 4
        [e] => 5
        [f] => 6
    )

[2] => Array
    (
        [g] => 7
        [h] => 8
        [i] => 9
    )

Isn't that what you're trying to get?

Sign up to request clarification or add additional context in comments.

4 Comments

this works I didn't realize this could be used like this, thank you much!
Yep: array_values() doesn't upset the lower levels of the array.
Your test data is not a multidimensional, keyed array. Your 2nd-depth arrays are already indexed from 0 up.
Okay, I've added keys to all dimensions. As you can see, array_values() still does not disturb the sub-level keys.
2

A little recursion does the trick

$data = array(
    'foo' => 'bar'
  , 'bar' => 'baz'
  , 'baz' => array(
        'foo' => 'bar'
      , 'bar' => 'baz'
      , 'baz' => array(
            'foo' => 'bar'
          , 'bar' => 'baz'
          , 'baz' => 'foo'
        )
    )
  , 'foo2' => 'bar'
  , 'bar2' => 'baz'
  , 'baz2' => array(
        'foo' => 'bar'
      , 'bar' => 'baz'
      , 'baz' => 'foo'
     )
);

print_r( $data );

$data = removeKeys( $data );

print_r( $data );

function removeKeys( array $array )
{
  $array = array_values( $array );
  foreach ( $array as &$value )
  {
    if ( is_array( $value ) )
    {
      $value = removeKeys( $value );
    }
  }
  return $array;
}

1 Comment

"I have a multi-dimensional array which first set of keys are named and I want to change them into numbers like 0, 1, 2.." This would indicate that the original poster only wants to re-index the first level.
0

I didn't entirely understand the structure of your array, but you can iterate through a multidimensional associative array in a number of ways. Here's one that builds a numerically indexed array:

$multiArray = Array(/* stuff */);
$numericArray = Array();
foreach ($multiArray as $key => $val) {
    foreach ($val as $childKey => $childVal) {
        // do something else?
    }
    $numericArray []= $val;
}

Comments

0
$array = array("name"=>"sima", "lastname"=>"johansoon");

$newArray = array();
foreach($array as $key=>$value) {
    array_push($newArray, $value);

}
print_r($newArray);

1 Comment

Welcome to Stack Overflow. While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. How to Answer

Your Answer

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.