guagay_wk
guagay_wk

Reputation: 28050

How to remove the unnecessary information and restructure this associative array in php?

I have this php associative array which I created from a MySQL query. It looks like this;

        array(
                (int) 0 => array(
                        'items' => array(
                                'index_no' => '1'
                        ),
                        (int) 0 => array(
                                'NumItems' => '2'
                        )
                ),
                (int) 1 => array(
                        'items' => array(
                                'index_no' => '2'
                        ),
                        (int) 0 => array(
                                'NumItems' => '3'
                        )
                )     

It looks unnecessarily complicated. I would like to simplify it to look something like this;

        array(
                (int) 0 => array(
                                'index_no' => '1',
                                'NumItems' => '2'
                        )
                ),
                (int) 1 => array(
                                'index_no' => '2',
                                'NumItems' => '3'
                        )
                )     

How can this be done in php? I have been stuck on this problem for some time. I will post my answer if I have it. I would appreciate it if someone could give me some starting point. Thank you very much.

Upvotes: 0

Views: 77

Answers (4)

Sougata Bose
Sougata Bose

Reputation: 31749

$newArray = array();
foreach ($array as $items) {
    $temp = array('index_no' => $items['index_no']);
    $temp = array_merge($temp, $items[0]);
    $newArray[] = $temp;
}

it will add all the keys to the array under index - 0

Upvotes: 1

Ankit
Ankit

Reputation: 259

Here is solution for you.

$diffArray = array(
    (int) 0 => array(
        'items' => array(
            'index_no' => '1'
        ),
        (int) 0 => array(
            'NumItems' => '2'
        )
    ),
    (int) 1 => array(
        'items' => array(
            'index_no' => '2'
        ),
        (int) 0 => array(
            'NumItems' => '3'
        )
        ));

print_r($diffArray);

$getArray = array();
foreach ($diffArray as $simArray) {

    $getArray['index_no'][] = $simArray['items']['index_no'];
    $getArray['NumItems'][]= $simArray[0]['NumItems'];
}
print_r($getArray);

Upvotes: 2

v2solutions.com
v2solutions.com

Reputation: 1439

You can try this out:

$tempArray = array(
                (int) 0 => array(
                        'items' => array(
                                'index_no' => '1'
                        ),
                        (int) 0 => array(
                                'NumItems' => '2'
                        )
                ),
                (int) 1 => array(
                        'items' => array(
                                'index_no' => '2'
                        ),
                        (int) 0 => array(
                                'NumItems' => '3'
                        )
                ));

$newArray = array();
$i=0;
foreach($tempArray as $temp) {
    $newArray[$i]['index_no'] = $temp['items']['index_no'];
    $newArray[$i]['NumItems'] = $temp[0]['NumItems'];
    $i++;
}
print "<pre>";
print_r($newArray);

Upvotes: 3

sasi kanth
sasi kanth

Reputation: 2937

try this

<?php $res=array(array('item'=>1,'number'=>5),array('item'=>2,'number'=>56));
 $final_array =array();
 $i=0;
  foreach ($res as $val)
  {
     foreach($val as $key=>$val2)
     {
      $final_array[$i][$key] = $val2;

      }$i++;
    }

  print_r($final_array);
   ?>

Upvotes: 2

Related Questions