Push key and value pair into multi dimensional array [duplicate] - php

This question already has answers here:
How to push both value and key into PHP array
(21 answers)
Closed 10 months ago.
Please note that this is not a duplicate - but an extension of these questions below)
PHP array_push one array into another
array_push into a multi-dimensional array
I am trying to array_push into a multidimensional array but want to keep the array key of the second array.
Example:
<?php
$samplearray = array(
array('name' => "Joe Bloggs", 'age' => "30", 'sex' => "Male", 'title' => "Mr" ),
array('name' => "Jane Bloggs", 'age' => "30", 'sex' => "Female", 'title' => "Mrs" ),
array('name' => "Little Bloggs", 'age' => "10", 'sex' => "Male", 'title' => "Master" ),
);
array_push ($samplearray[0],"Inserted Value");
print_r($samplearray);
?>
The output for this is:
array(3) {
[0]=>
array(5) {
["name"]=>
string(10) "Joe Bloggs"
["age"]=>
string(2) "30"
["sex"]=>
string(4) "Male"
["title"]=>
string(2) "Mr"
[0]=>
string(14) "Inserted Value" <-- INSERTED VALUE
}
[1]=>
array(4) {
["name"]=>
string(11) "Jane Bloggs"
["age"]=>
string(2) "30"
["sex"]=>
string(6) "Female"
["title"]=>
string(3) "Mrs"
}
[2]=>
array(4) {
["name"]=>
string(13) "Little Bloggs"
["age"]=>
string(2) "10"
["sex"]=>
string(4) "Male"
["title"]=>
string(6) "Master"
}
}
I want to insert a key along with the value but when I try that - it fails. Can you please advice
array_push ($samplearray[0]['insertedvalue'],"Inserted Value");
returns a NULL value for the inserted key on local server but fails on PHPfiddle.

is this what you are looking for?
$samplearray = array(
array('name' => "Joe Bloggs", 'age' => "30", 'sex' => "Male", 'title' => "Mr" ),
array('name' => "Jane Bloggs", 'age' => "30", 'sex' => "Female", 'title' => "Mrs" ),
array('name' => "Little Bloggs", 'age' => "10", 'sex' => "Male", 'title' => "Master" ),
);
$samplearray[0]['othername'] = 'lalala';
echo '<pre>';
print_r($samplearray);
and this should print:
Array
(
[0] => Array
(
[name] => Joe Bloggs
[age] => 30
[sex] => Male
[title] => Mr
[othername] => lalala
)
[1] => Array
(
[name] => Jane Bloggs
[age] => 30
[sex] => Female
[title] => Mrs
)
[2] => Array
(
[name] => Little Bloggs
[age] => 10
[sex] => Male
[title] => Master
)
)

This must be what you need.
$samplearray[0]['insertedvalue'] = "Inserted Value";

Related

Convert CSV to object array in PHP

Hi I am creating a function that pulls my uploaded CSV via ajax then I would like to form an json object that can be posted to the woocommerce API.
The first loop in the while loop creates my labels array and every loop through it is using the labels as a key.
if ($file !== FALSE) {
while (($data = fgetcsv($file, 0, ",")) !== FALSE):
// print_r($data);
if($row == 1) {
$labels = $data;
} else {
$new_data = array_combine($labels, $data);
array_push($csv_data, $new_data);
}
$row++;
endwhile;
foreach($csv_data as $data){
echo "New Customer: ";
print_r($data);
}
fclose($file);
}
When I run this code I get this result:
New Customer: Array
(
[Name] => Test Dude
[Username] => Test
[Email] => test#johndeer.com
[Orders] => 1
[proceed_id] => 765
[AOV] => 765
[Country / Region] => US
[City] => Testerton
[Region] => AR
[Postal Code] => 71601
[Address] => John Drive
)
New Customer: Array
(
[Name] => John Deer
[Username] => johndeer
[Email] => johndeer#jonnydeers.com
[Orders] => 0
[proceed_id] => 0
[AOV] => 0
[Country / Region] => CA
[City] => Testerton
[Region] => AB
[Postal Code] => T6M 2V5
[Address] => John Street
)
New Customer: Array
(
[Name] => John Doe
[Username] => john.doe
[Email] => john.doe#example.com
[Orders] => 0
[proceed_id] => 0
[AOV] => 0
[Country / Region] => US
[City] => San Francisco
[Region] => CA
[Postal Code] => 94103
[Address] => John Drive
)
New Customer: Array
(
[Name] => John Doe
[Username] => johndoe
[Email] => johndoe#example.com
[Orders] => 0
[proceed_id] => 0
[AOV] => 0
[Country / Region] => US
[City] => San Francisco
[Region] => CA
[Postal Code] => 94103
[Address] =>
)
New Customer: Array
(
[Name] => John Doe
[Username] => johndoe1
[Email] => johndoe123#example.com
[Orders] => 0
[proceed_id] => 0
[AOV] => 0
[Country / Region] => US
[City] => San Francisco
[Region] => CA
[Postal Code] => 94103
[Address] =>
)
New Customer: Array
(
[Name] => John Doe
[Username] => johndoe7
[Email] => johndoe1234#example.com
[Orders] => 0
[proceed_id] => 0
[AOV] => 0
[Country / Region] => US
[City] => San Francisco
[Region] => CA
[Postal Code] => 94103
[Address] =>
)
New Customer: Array
(
[Name] => John Doe
[Username] => johndoe5
[Email] => johndoe5#example.com
[Orders] => 0
[proceed_id] => 0
[AOV] => 0
[Country / Region] => US
[City] => San Francisco
[Region] => CA
[Postal Code] => 94103
[Address] =>
)
New Customer: Array
(
[Name] => John Doe
[Username] => johndoe6
[Email] => johndoe6#example.com
[Orders] => 0
[proceed_id] => 0
[AOV] => 0
[Country / Region] => US
[City] => San Francisco
[Region] => CA
[Postal Code] => 94103
[Address] =>
)
New Customer: Array
(
[Name] => John Doe
[Username] => johndoe8
[Email] => johndoe8#example.com
[Orders] => 0
[proceed_id] => 0
[AOV] => 0
[Country / Region] => US
[City] => San Francisco
[Region] => CA
[Postal Code] => 94103
[Address] =>
)
0
It displays New Customer: in each loop just to show that is one loop cycle. However when I try to select "Name" or "proceed_id" it returns null. I've tried to use both of these syntaxes $data['Name] and $data->Name. How do I select by key inside this foreach?
foreach($csv_data as $data){
//$data is the individual rows
$proceed_id = $data['proceed_id'];
}
EDIT 1:
Here is the vardump of $csv_data outside loop.
array(9) {
[0]=>
array(11) {
["Name"]=>
string(9) "Test Dude"
["Username"]=>
string(4) "Test"
["Email"]=>
string(17) "test#johndeer.com"
["Orders"]=>
string(1) "1"
["proceed_id"]=>
string(3) "765"
["AOV"]=>
string(3) "765"
["Country / Region"]=>
string(2) "US"
["City"]=>
string(9) "Testerton"
["Region"]=>
string(2) "AR"
["Postal Code"]=>
string(5) "71601"
["Address"]=>
string(10) "John Drive"
}
[1]=>
array(11) {
["Name"]=>
string(9) "John Deer"
["Username"]=>
string(8) "johndeer"
["Email"]=>
string(23) "johndeer#jonnydeers.com"
["Orders"]=>
string(1) "0"
["proceed_id"]=>
string(1) "0"
["AOV"]=>
string(1) "0"
["Country / Region"]=>
string(2) "CA"
["City"]=>
string(9) "Testerton"
["Region"]=>
string(2) "AB"
["Postal Code"]=>
string(7) "T6M 2V5"
["Address"]=>
string(11) "John Street"
}
[2]=>
array(11) {
["Name"]=>
string(8) "John Doe"
["Username"]=>
string(8) "john.doe"
["Email"]=>
string(20) "john.doe#example.com"
["Orders"]=>
string(1) "0"
["proceed_id"]=>
string(1) "0"
["AOV"]=>
string(1) "0"
["Country / Region"]=>
string(2) "US"
["City"]=>
string(13) "San Francisco"
["Region"]=>
string(2) "CA"
["Postal Code"]=>
string(5) "94103"
["Address"]=>
string(10) "John Drive"
}
[3]=>
array(11) {
["Name"]=>
string(8) "John Doe"
["Username"]=>
string(7) "johndoe"
["Email"]=>
string(19) "johndoe#example.com"
["Orders"]=>
string(1) "0"
["proceed_id"]=>
string(1) "0"
["AOV"]=>
string(1) "0"
["Country / Region"]=>
string(2) "US"
["City"]=>
string(13) "San Francisco"
["Region"]=>
string(2) "CA"
["Postal Code"]=>
string(5) "94103"
["Address"]=>
string(0) ""
}
[4]=>
array(11) {
["Name"]=>
string(8) "John Doe"
["Username"]=>
string(8) "johndoe1"
["Email"]=>
string(22) "johndoe123#example.com"
["Orders"]=>
string(1) "0"
["proceed_id"]=>
string(1) "0"
["AOV"]=>
string(1) "0"
["Country / Region"]=>
string(2) "US"
["City"]=>
string(13) "San Francisco"
["Region"]=>
string(2) "CA"
["Postal Code"]=>
string(5) "94103"
["Address"]=>
string(0) ""
}
[5]=>
array(11) {
["Name"]=>
string(8) "John Doe"
["Username"]=>
string(8) "johndoe7"
["Email"]=>
string(23) "johndoe1234#example.com"
["Orders"]=>
string(1) "0"
["proceed_id"]=>
string(1) "0"
["AOV"]=>
string(1) "0"
["Country / Region"]=>
string(2) "US"
["City"]=>
string(13) "San Francisco"
["Region"]=>
string(2) "CA"
["Postal Code"]=>
string(5) "94103"
["Address"]=>
string(0) ""
}
[6]=>
array(11) {
["Name"]=>
string(8) "John Doe"
["Username"]=>
string(8) "johndoe5"
["Email"]=>
string(20) "johndoe5#example.com"
["Orders"]=>
string(1) "0"
["proceed_id"]=>
string(1) "0"
["AOV"]=>
string(1) "0"
["Country / Region"]=>
string(2) "US"
["City"]=>
string(13) "San Francisco"
["Region"]=>
string(2) "CA"
["Postal Code"]=>
string(5) "94103"
["Address"]=>
string(0) ""
}
[7]=>
array(11) {
["Name"]=>
string(8) "John Doe"
["Username"]=>
string(8) "johndoe6"
["Email"]=>
string(20) "johndoe6#example.com"
["Orders"]=>
string(1) "0"
["proceed_id"]=>
string(1) "0"
["AOV"]=>
string(1) "0"
["Country / Region"]=>
string(2) "US"
["City"]=>
string(13) "San Francisco"
["Region"]=>
string(2) "CA"
["Postal Code"]=>
string(5) "94103"
["Address"]=>
string(0) ""
}
[8]=>
array(11) {
["Name"]=>
string(8) "John Doe"
["Username"]=>
string(8) "johndoe8"
["Email"]=>
string(20) "johndoe8#example.com"
["Orders"]=>
string(1) "0"
["proceed_id"]=>
string(1) "0"
["AOV"]=>
string(1) "0"
["Country / Region"]=>
string(2) "US"
["City"]=>
string(13) "San Francisco"
["Region"]=>
string(2) "CA"
["Postal Code"]=>
string(5) "94103"
["Address"]=>
string(0) ""
}
}
0
Var_export() =
array (
0 =>
array (
'Name' => 'Test Dude',
'Username' => 'Test',
'Email' => 'test#johndeer.com',
'Orders' => '1',
'proceed_id' => '765',
'AOV' => '765',
'Country / Region' => 'US',
'City' => 'Testerton',
'Region' => 'AR',
'Postal Code' => '71601',
'Address' => 'John Drive',
),
1 =>
array (
'Name' => 'John Deer',
'Username' => 'johndeer',
'Email' => 'johndeer#jonnydeers.com',
'Orders' => '0',
'proceed_id' => '0',
'AOV' => '0',
'Country / Region' => 'CA',
'City' => 'Testerton',
'Region' => 'AB',
'Postal Code' => 'T6M 2V5',
'Address' => 'John Street',
),
2 =>
array (
'Name' => 'John Doe',
'Username' => 'john.doe',
'Email' => 'john.doe#example.com',
'Orders' => '0',
'proceed_id' => '0',
'AOV' => '0',
'Country / Region' => 'US',
'City' => 'San Francisco',
'Region' => 'CA',
'Postal Code' => '94103',
'Address' => 'John Drive',
),
3 =>
array (
'Name' => 'John Doe',
'Username' => 'johndoe',
'Email' => 'johndoe#example.com',
'Orders' => '0',
'proceed_id' => '0',
'AOV' => '0',
'Country / Region' => 'US',
'City' => 'San Francisco',
'Region' => 'CA',
'Postal Code' => '94103',
'Address' => '',
),
4 =>
array (
'Name' => 'John Doe',
'Username' => 'johndoe1',
'Email' => 'johndoe123#example.com',
'Orders' => '0',
'proceed_id' => '0',
'AOV' => '0',
'Country / Region' => 'US',
'City' => 'San Francisco',
'Region' => 'CA',
'Postal Code' => '94103',
'Address' => '',
),
5 =>
array (
'Name' => 'John Doe',
'Username' => 'johndoe7',
'Email' => 'johndoe1234#example.com',
'Orders' => '0',
'proceed_id' => '0',
'AOV' => '0',
'Country / Region' => 'US',
'City' => 'San Francisco',
'Region' => 'CA',
'Postal Code' => '94103',
'Address' => '',
),
6 =>
array (
'Name' => 'John Doe',
'Username' => 'johndoe5',
'Email' => 'johndoe5#example.com',
'Orders' => '0',
'proceed_id' => '0',
'AOV' => '0',
'Country / Region' => 'US',
'City' => 'San Francisco',
'Region' => 'CA',
'Postal Code' => '94103',
'Address' => '',
),
7 =>
array (
'Name' => 'John Doe',
'Username' => 'johndoe6',
'Email' => 'johndoe6#example.com',
'Orders' => '0',
'proceed_id' => '0',
'AOV' => '0',
'Country / Region' => 'US',
'City' => 'San Francisco',
'Region' => 'CA',
'Postal Code' => '94103',
'Address' => '',
),
8 =>
array (
'Name' => 'John Doe',
'Username' => 'johndoe8',
'Email' => 'johndoe8#example.com',
'Orders' => '0',
'proceed_id' => '0',
'AOV' => '0',
'Country / Region' => 'US',
'City' => 'San Francisco',
'Region' => 'CA',
'Postal Code' => '94103',
'Address' => '',
),
)

PHP Array manipulation need tips

I have arrays in one submission, please see below details:
array(5) {
["ambition_id"]=>
array(2) {
[55]=> string(2) "55"
[60]=> string(2) "60"
}
["target"]=>
array(1) {
[0]=> string(8) "target 1"
[1]=> string(8) "target 2"
}
["strides"]=>
array(1) {
[0]=> string(1) "1"
[1]=> string(1) "1"
}
["date"]=>
array(1) {
[0]=> string(10) "2017-02-08"
[1]=> string(10) "2017-03-08"
}
["frequency"]=>
array(1) {
[0]=> string(1) "1"
[1]=> string(1) "2"
}
}
Actually, I have two tables in mysql, 'ambition' and 'target'. Ambition is a group of targets ('ambition_id' is foreign key in 'target' table). That array will be stored in 'target' table. That's why there is an 'ambition_id'
I've tried many times but failed (using foreach), now I need someone who can give me a help.
By brute force, It's easy! I solved it already but I need "more advanced" array manipulation.
How can I come up into this?
array(2) {
[0] => array('ambition_id' => 55,
'target' => 'target 1',
'strides' => 1,
'date' => '2017-02-08',
'frequency' => 1
),
[1] => array('ambition_id' => 60,
'target' => 'target 2',
'strides' => 2,
'date' => '2017-03-08',
'frequency' => 2)
}
Please do help, many thanks!
You have to pivot your data:
$data = array (
"ambition_id" =>
array (
55 => "55",
60 => "60"
),
"target" =>
array (
0 => "target 1",
1 => "target 2"
),
"strides" =>
array (
0 => "1",
1 => "1"
),
"date" =>
array (
0 => "2017-02-08",
1 => "2017-03-08"
),
"frequency" =>
array (
0 => "1",
1 => "2"
)
);
// pivot data
$pivot = array();
foreach ($data as $datum => $values) {
$value_index = 0;
foreach ($values as $value) {
$pivot[$value_index][$datum] = $value;
$value_index++;
}
}
print_r($pivot);
This assumes you only have two levels of data and that the data is well behaved.
Not the best answer, but it solves your problem
<?php
$array = [
"ambition_id" =>
[
55 => "55",
60 => "60"
],
"target" =>
[
0 => "target 1",
1 => "target 2"
],
"strides" =>
[
0 => "1",
1 => "1"
],
"date" =>
[
0 => "2017-02-08",
1 => "2017-03-08"
],
"frequency" =>
[
0 => "1",
1 => "2"
],
];
$result = array();
foreach ($array as $k => $v) {
foreach ($v as $kk => $vv) {
if ($k == "ambition_id") {
$result[] = array($k => $vv);
} else {
$result[$kk][$k] = $vv;
}
}
}
Here is the test https://3v4l.org/UdHH8
Just use loop the array and user array_values to re-index the loop the inner array and store it into new array like below .
<?php
$new_array =array();
foreach($array as $key1=>$row1 )
{
$ss =array_values($row1);
foreach($ss as $key2=>$row2)
{
$new_array[$key2][$key1]=$row2;
}
}
echo "<pre>";
print_r($new_array);
?>
Output :
Array
(
[0] => Array
(
[ambition_id] => 55
[target] => target 1
[strides] => 1
[date] => 2017-02-08
[frequency] => 1
)
[1] => Array
(
[ambition_id] => 60
[target] => target 2
[strides] => 1
[date] => 2017-03-08
[frequency] => 2
)
)

PHP array merge with keys

How do I merge these two arrays:
Array
(
[uczrrtawpxfjanycwwlqygoq] => Array
(
[user_id] => 53
[value] => Boris
[key] => uczrrtawpxfjanycwwlqygoq
)
[dreamhack] => Array
(
[user_id] => 263
[value] => More
[key] => dreamhack
)
)
And my second array which needs to be added to the keys of the first
Array
(
[dreamhack] => Array
(
[viewers] => 32229
[channel] => Array
(
[broadcaster_language] => en
[display_name] => Dreamhack
[_id] => 22859340
[created_at] => 2011-06-09T06:11:52Z
[updated_at] => 2016-08-14T18:34:36Z
[delay] =>
[banner] =>
[background] =>
[partner] => 1
[views] => 36258931
[followers] => 79892
[_links] => Array
(
[self] =>
[teams] =>
)
)
)
)
Doing a simple array merge gives the original array and not a combined one. So for dreamhack I would require one aeeay with all the tags combined [user_id], [value], [key], [viewers], [channel] and subarray.
as asked in the comment .. is this what you want ?
<pre>
<?php
$array1 = [
'uczrrtawpxfjanycwwlqygoq' => [
'user_id' => 53,
'value' => 'Boris',
'key' => 'uczrrtawpxfjanycwwlqygoq'
],
'dreamhack' => [
'user_id' => 263,
'value' => 'More',
'key' => 'dreamhack'
]
];
$array2 = [
'dreamhack' => [
'viewers' => 32229,
'channel' => [
'broadcaster_language' => 'en',
'display_name' => 'Dreamhack',
'_id' => 22859340,
'created_at' => '2011-06-09T06:11:52Z',
'updated_at' => '2016-08-14T18:34:36Z',
'delay' => '',
'banner' => '',
'background' => '',
'partner' => 1,
'views' => 36258931,
'followers' => 79892,
'_links' => [
'self' => '',
'teams' => ''
]
]
]
];
$result = array_merge_recursive ($array1, $array2);
var_dump($result);
?>
</pre>
result looks like:
array(2) {
["uczrrtawpxfjanycwwlqygoq"]=>
array(3) {
["user_id"]=>
int(53)
["value"]=>
string(5) "Boris"
["key"]=>
string(24) "uczrrtawpxfjanycwwlqygoq"
}
["dreamhack"]=>
array(5) {
["user_id"]=>
int(263)
["value"]=>
string(4) "More"
["key"]=>
string(9) "dreamhack"
["viewers"]=>
int(32229)
["channel"]=>
array(12) {
["broadcaster_language"]=>
string(2) "en"
["display_name"]=>
string(9) "Dreamhack"
["_id"]=>
int(22859340)
["created_at"]=>
string(20) "2011-06-09T06:11:52Z"
["updated_at"]=>
string(20) "2016-08-14T18:34:36Z"
["delay"]=>
string(0) ""
["banner"]=>
string(0) ""
["background"]=>
string(0) ""
["partner"]=>
int(1)
["views"]=>
int(36258931)
["followers"]=>
int(79892)
["_links"]=>
array(2) {
["self"]=>
string(0) ""
["teams"]=>
string(0) ""
}
}
}
}
Use array_merge_recursive, which is specifically designed to do this sort of thing. To quote the docs:
array_merge_recursive() merges the elements of one or more arrays
together so that the values of one are appended to the end of the
previous one. It returns the resulting array.
If the input arrays have the same string keys, then the values for
these keys are merged together into an array, and this is done
recursively, so that if one of the values is an array itself, the
function will merge it with a corresponding entry in another array
too. If, however, the arrays have the same numeric key, the later
value will not overwrite the original value, but will be appended.

How to insert data into php multi dimensional array

This one should be straight forward, but I am having issues as I have not worked with arrays that much.
So I am try to insert data into a 3 dimensional array, this is the structure of my 3 dimensional array:
Array
(
[data] => Array
(
[0] => Array
(
[name] => name
[by] => by
[imgUrl] => imgUrl
[linkUrl] => linkUrl
[date] => date
)
[1] => Array
(
[name] => name
[by] => by
[imgUrl] => imgUrl
[linkUrl] => linkUrl
[date] => date
)
)
)
I am trying to push the existing array downwards, the existing [0] becomes [1], ect. While the new [0] will be the posted data from a form.
I have tried array_push, array_splice, array_merge, but all to no avail.
if I understood you correctly...
here's a fiddle.
$multi = array(
"data" =>array(
array(
'something1' => 'something else',
'something0' => 'something else',
'something345' => 'something else'
),
array(
'something1' => 'something else',
'something0' => 'something else',
'something345' => 'something else'
),
)
);
$new = array(
'something1' => 'something else',
'something0' => 'something else',
'something345' => 'something else'
);
array_push($multi['data'], $new);
print_r($multi);
You are looking for the array_unshift function:
array_unshift($arr["data"], $new);
Test script:
$arr = Array(
"data" => Array(
Array(
"name" => "name",
"by" => "by",
"imgUrl" => "imgUrl",
"linkUrl" => "linkUrl",
"date" => "date"
)
,Array(
"name" => "lastname",
"by" => "by",
"imgUrl" => "imgUrl",
"linkUrl" => "linkUrl",
"date" => "date"
)
)
);
$new = Array(
"name" => "newname",
"by" => "newby",
"imgUrl" => "newimgUrl",
"linkUrl" => "newlinkUrl",
"date" => "newdate"
);
array_unshift($arr["data"], $new);
print_r ($arr);
Output shows that new element pushes the other elements down:
array(1) {
["data"]=> array(3) {
[0]=> array(5) {
["name"]=> string(7) "newname"
["by"]=> string(5) "newby"
["imgUrl"]=> string(9) "newimgUrl"
["linkUrl"]=> string(10) "newlinkUrl"
["date"]=> string(7) "newdate"
}
[1]=> array(5) {
["name"]=> string(4) "name"
["by"]=> string(2) "by"
["imgUrl"]=> string(6) "imgUrl"
["linkUrl"]=> string(7) "linkUrl"
["date"]=> string(4) "date"
}
[2]=> array(5) {
["name"]=> string(9) "firstname"
["by"]=> string(2) "by"
["imgUrl"]=> string(6) "imgUrl"
["linkUrl"]=> string(7) "linkUrl"
["date"]=> string(4) "date"
}
}
}

array key value pair at a specified point in array

I have an associative array , i would like to add some more key and values
Array
(
[0] => Array
(
[NUMBER] => 67
[TYPE] => Other
[DATE] => 3/31/2011
)
[1] => Array
(
[NUMBER] => 87
[TYPE] => something
[DATE] => 3/28/2011
)
[2] => Array
(
[NUMBER] => 67
[TYPE] => Other
[DATE] => 3/2/2011
)
)
In Above array i want to add another key named STATUS and value before DATE
so that finally iget
Array
(
[0] => Array
(
[NUMBER] => 67
[TYPE] => Other
[STATUS] => waiting
[DATE] => 3/31/2011
)
}
canPlease give me proper direction
$arr = Array(
0 => Array('NUMBER' => 67, 'TYPE' => Other, 'DATE' => '3/32/2011'),
1 => Array('NUMBER' => 87, 'TYPE' => something, 'DATE' => '3/28/2011'),
2 => Array('NUMBER' => 67, 'TYPE' => Other, 'DATE' => '3/2/2011')
);
foreach($arr as $key => $value) {
$arr[$key] = array_slice($value, 0, 2) +
array('Status' => 'waiting') +
array_slice($value, -1);
}
var_dump($arr);
gives the following array:
array(3) {
[0]=>
array(4) {
["NUMBER"]=>
int(67)
["TYPE"]=>
string(5) "Other"
["Status"]=>
string(7) "waiting"
["DATE"]=>
string(9) "3/32/2011"
}
[1]=>
array(4) {
["NUMBER"]=>
int(87)
["TYPE"]=>
string(9) "something"
["Status"]=>
string(7) "waiting"
["DATE"]=>
string(9) "3/28/2011"
}
[2]=>
array(4) {
["NUMBER"]=>
int(67)
["TYPE"]=>
string(5) "Other"
["Status"]=>
string(7) "waiting"
["DATE"]=>
string(8) "3/2/2011"
}
}

Categories