开发者

PHP Array Manipulation

array
  324545432 => 
    array
      0 => 
        array
          'Age' => int 1
          'Status' => string 'INSTALLED' 
          'Id' => string '830026495' 
          'name' => string 'TV' 
      1 => 
        array
          'Age' => int 2
          'Status' => string 'GOOD' 
          'Id' => string '553718299' 
          'name' => string 'Computer'

O/P Array should be of Structure:

    array
      0 => 
        array
          'Age' => int 0
          'Status' => string 'INSTALLED/GOOD' 
          'Id' => string '830026495' 
          'name' => string 'TV' 
      1 => 
        array
          'Age' => int 0
          'Status' => string 'INSTALLED/GOOD' 
          'Id' => string '553718299' 
          'name' => string 'Computer'               

Update:

Input Array:

array
  324545433 => 
    array
      0 => 
        array
          'Age' => int 0
          'Status' => string 'INSTALLED/GOOD' 
          'Id' => string '830026495' 
          'name' => string 'TV' 
      1 => 
        array
          'Age' => int 0
          'Status' => string 'INSTALLED/GOOD' 
          'Id' => string '553718299' 
          'name' => string 'Computer'         
array
  324545432 => 
        array
          0 => 
            array
              'Age' => int 0
              'Status' => string 'INSTALLED/GOOD' 
              'Id' => string '830026495' 
              'name' => string 'TV' 
          1 => 
            array
              'Age' => int 0
              'Status' => string 'INSTALLED/GOOD' 
              'Id' => string '553718299' 
              'name' => string 'Computer'
array
  324545431 => 
        array
          0 => 
            array
              'Age' => int 0
              'Status' => string 'INSTALLED/GOOD' 
              'Id' => string '830026495' 
              'name' => string 'TV' 
          1 => 
            array
              'Age' => int 0
              'Status' => string 'INSTALLED/GOOD' 
              'Id' => string '553718299' 
              'name' => string 'Computer'
array
  324545434 => 
        array
          0 => 
            array
              'Age' => int 0
              'Status' => string 'INSTALLED/GOOD' 
              'Id' => string '830026495' 
              'name' => string 'TV' 
          1 => 
            array
              'Age' => int 0
              'Status' => string 'INSTALLED/GOOD' 
              'Id' => string '553718299' 
              'name' => string 'Computer'
array
  324545430 => 
        array
          0 => 
            array
              'Age' => int 0
              'Status' => string 'INSTALLED/GOOD' 
              'Id' => string '830026495' 
              'name' => string 'TV' 
          1 => 
            array
   开发者_开发知识库           'Age' => int 0
              'Status' => string 'INSTALLED/GOOD' 
              'Id' => string '553718299' 
              'name' => string 'Computer'

. . . . .

Output Required:

    array
      0 => 
        array
          'Age' => int 0
          'Status' => string 'INSTALLED/GOOD' 
          'Id' => string '830026495' 
          'name' => string 'TV' 
      1 => 
        array
          'Age' => int 0
          'Status' => string 'INSTALLED/GOOD' 
          'Id' => string '553718299' 
          'name' => string 'Computer'
     2 => 
        array
          'Age' => int 0
          'Status' => string 'INSTALLED/GOOD' 
          'Id' => string '830026495' 
          'name' => string 'TV' 
      3 => 
        array
          'Age' => int 0
          'Status' => string 'INSTALLED/GOOD' 
          'Id' => string '553718299' 
          'name' => string 'Computer'
      4 => 
        array
          'Age' => int 0
          'Status' => string 'INSTALLED/GOOD' 
          'Id' => string '830026495' 
          'name' => string 'TV' 
      5 => 
        array
          'Age' => int 0
          'Status' => string 'INSTALLED/GOOD' 
          'Id' => string '553718299' 
          'name' => string 'Computer'
      6 => 
        array
          'Age' => int 0
          'Status' => string 'INSTALLED/GOOD' 
          'Id' => string '830026495' 
          'name' => string 'TV' 
      7 => 
        array
          'Age' => int 0
          'Status' => string 'INSTALLED/GOOD' 
          'Id' => string '553718299' 
          'name' => string 'Computer'     
      8 => 
        array
          'Age' => int 0
          'Status' => string 'INSTALLED/GOOD' 
          'Id' => string '830026495' 
          'name' => string 'TV' 
      9 => 
        array
          'Age' => int 0
          'Status' => string 'INSTALLED/GOOD' 
          'Id' => string '553718299' 
          'name' => string 'Computer'

How can we achieve this ? Thing to note is that we can have many layers of '3242524621' like numbers which are associative array.

Thanks.


$k = array_keys($array);
return isset($k[0]) ? $array[$k[0]] : array();

Here you are :)


This will set $new_array to the first value of $original_array no matter what the key is.

 function get_first($original_array)
 {
    foreach($original_array as $array)
    {
        return $array;
    }
}
$array = get_first($other_array);

You could also try array_shift() which will get the first value of $original_array, remove if from $original_array and return it, thus that the removed value is now set to $new_array

$new_array = array_shift($original_array);

You could probably make another function similar to the top one that would get rid of the destructive element.

function get_first($array)
{
    return array_shift($array);
}

Because arrays are passed by value and not reference, the array you pass into that function will remain intact.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜