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"
}
}
Related
I have an
array() {
[0] =>
string(1) "id1"
[1] =>
string(14) "Gucci"
[2] =>
string(1) "id1"
[3] =>
string(24) "bag1"
[4] =>
string(4) "id2"
[5] =>
string(14) "Gucci"
[6] =>
string(4) "id2"
[7] =>
string(25) "Gucci bag2"
[8] =>
string(4) "id3"
[9] =>
string(14) "Gucci"
[10] =>
string(4) "id3"
[11] =>
string(27) "bag3"
..more
the key 0, 2, 4, ...are the id
the key 1, 5 .. are the brand
the key 3, 6 are the name
i want to loop trough the array to check if $array[1] is in $array[3], and then $array[5] is in $array[7]
how can i have the iteration of
if (preg_match("/$array[$i+1]/i", "$array[$i+3]")) {
echo "$array[$i]";
} else {
echo $array[$i+1] . ' ' . $array[$i+3];
}
Thank you
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.
I have the following query result:
Array
(
[0] => stdClass Object
(
[TreatmentLog_ID] => 131
[DateAdministered] => 2016-07-15
[Notes] =>
[Treatment_ID] => 144
[AmountAdministered] => 1.5
[Injectable_ID] => 2
[InjectableName] => Baytril
)
[1] => stdClass Object
(
[TreatmentLog_ID] => 133
[DateAdministered] => 2016-07-12
[Notes] =>
[Treatment_ID] => 146
[AmountAdministered] => 1.2
[Injectable_ID] => 20
[InjectableName] => Vitamin C
)
[2] => stdClass Object
(
[TreatmentLog_ID] => 133
[DateAdministered] => 2016-07-12
[Notes] =>
[Treatment_ID] => 147
[AmountAdministered] => 1.3
[Injectable_ID] => 21
[InjectableName] => Vitamin E
)
)
I'd like to be able to restructure the array into something like this:
Array
(
[0] => stdClass Object
(
[TreatmentLog_ID] => 131
[DateAdministered] => 2016-07-15
[Notes] =>
[Treatments] => Array
(
[0] => stdClass Object
(
[Treatment_ID] => 144
[AmountAdministered] => 1.5
[Injectable_ID] => 2
[InjectableName] => Baytril
)
)
)
[1] => stdClass Object
(
[TreatmentLog_ID] => 133
[DateAdministered] => 2016-07-12
[Notes] =>
[Treatments] => Array
(
[0] => stdClass Object
(
[Treatment_ID] => 146
[AmountAdministered] => 1.2
[Injectable_ID] => 20
[InjectableName] => Vitamin C
)
[1] => stdClass Object
(
[Treatment_ID] => 147
[AmountAdministered] => 1.3
[Injectable_ID] => 21
[InjectableName] => Vitamin E
)
)
)
)
Notice how the second array looks merges the InjectableName, AmountAdministered, Injectable_ID, and Treatment_ID into the array Treatments if the TreatmentLog_ID is a match. Typically working with arrays isn't a problem, but this one has me stumped. Also I cannot change the query.
How could I pull this off in PHP?
The solution using isset and array_values functions:
// $arr is your initial array
$result = [];
foreach ($arr as $obj) {
$innerObj = (object)[ 'Treatment_ID' => $obj->Treatment_ID, 'AmountAdministered' => $obj->AmountAdministered,
'Injectable_ID' => $obj->Injectable_ID, 'InjectableName' => $obj->InjectableName ];
if (!isset($result[$obj->TreatmentLog_ID])) {
$result[$obj->TreatmentLog_ID] = (object)[
'TreatmentLog_ID' => $obj->TreatmentLog_ID,
'DateAdministered' => $obj->DateAdministered,
'Notes' => $obj->Notes,
'Treatments' => [$innerObj]
];
} else {
$result[$obj->TreatmentLog_ID]->Treatments[] = $innerObj;
}
}
$result = array_values($result);
print_r($result); // will output the expected result
Try this.
We use array_filter() to fetch all of the elements from the $inputArray with the same TreatmentLog_ID. Then we transform those filtered elements with array_map(). We have to create a copy of each element with clone, since they're objects and objects are passed by reference. Then we unset() the keys we don't need in the copy.
<?php
$inputArray = [
0 => (object) [
'TreatmentLog_ID' => 131,
'DateAdministered' => '2016-07-15',
'Notes' => '',
'Treatment_ID' => 144,
'AmountAdministered' => 1.5,
'Injectable_ID' => 2,
'InjectableName' => 'Baytril'
],
1 => (object) [
'TreatmentLog_ID' => 133,
'DateAdministered' => '2016-07-12',
'Notes' => '',
'Treatment_ID' => 146,
'AmountAdministered' => 1.2,
'Injectable_ID' => 20,
'InjectableName' => 'Vitamin C'
],
2 => (object) [
'TreatmentLog_ID' => 133,
'DateAdministered' => '2016-07-12',
'Notes' => '',
'Treatment_ID' => 147,
'AmountAdministered' => 1.3,
'Injectable_ID' => 21,
'InjectableName' => 'Vitamin E'
],
];
$transformedArray = [];
foreach ($inputArray as $key => $value)
{
$transformedArray[$key] = [
'TreatmentLog_ID' => $value->TreatmentLog_ID,
'DateAdministered' => $value->DateAdministered,
'Notes' => $value->Notes,
'Treatments' => array_map(
function ($v) {
$copy = clone $v;
unset($copy->Notes);
unset($copy->DateAdministered);
unset($copy->TreatmentLog_ID);
return $copy;
},
array_filter($inputArray, function ($v) use ($value) {
return $v->TreatmentLog_ID == $value->TreatmentLog_ID;
})
)
];
}
var_dump($transformedArray);
This gives me:
array(3) {
[0]=>
array(4) {
["TreatmentLog_ID"]=>
int(131)
["DateAdministered"]=>
string(10) "2016-07-15"
["Notes"]=>
string(0) ""
["Treatments"]=>
array(1) {
[0]=>
object(stdClass)#5 (4) {
["Treatment_ID"]=>
int(144)
["AmountAdministered"]=>
float(1.5)
["Injectable_ID"]=>
int(2)
["InjectableName"]=>
string(7) "Baytril"
}
}
}
[1]=>
array(4) {
["TreatmentLog_ID"]=>
int(133)
["DateAdministered"]=>
string(10) "2016-07-12"
["Notes"]=>
string(0) ""
["Treatments"]=>
array(2) {
[1]=>
object(stdClass)#6 (4) {
["Treatment_ID"]=>
int(146)
["AmountAdministered"]=>
float(1.2)
["Injectable_ID"]=>
int(20)
["InjectableName"]=>
string(9) "Vitamin C"
}
[2]=>
object(stdClass)#7 (4) {
["Treatment_ID"]=>
int(147)
["AmountAdministered"]=>
float(1.3)
["Injectable_ID"]=>
int(21)
["InjectableName"]=>
string(9) "Vitamin E"
}
}
}
[2]=>
array(4) {
["TreatmentLog_ID"]=>
int(133)
["DateAdministered"]=>
string(10) "2016-07-12"
["Notes"]=>
string(0) ""
["Treatments"]=>
array(2) {
[1]=>
object(stdClass)#8 (4) {
["Treatment_ID"]=>
int(146)
["AmountAdministered"]=>
float(1.2)
["Injectable_ID"]=>
int(20)
["InjectableName"]=>
string(9) "Vitamin C"
}
[2]=>
object(stdClass)#9 (4) {
["Treatment_ID"]=>
int(147)
["AmountAdministered"]=>
float(1.3)
["Injectable_ID"]=>
int(21)
["InjectableName"]=>
string(9) "Vitamin E"
}
}
}
}
Let's break down how we build Treatments:
'Treatments' => array_map(
function ($v) {
$copy = clone $v;
unset($copy->Notes);
unset($copy->DateAdministered);
unset($copy->TreatmentLog_ID);
return $copy;
},
array_filter($inputArray, function ($v) use ($value) {
return $v->TreatmentLog_ID == $value->TreatmentLog_ID;
})
)
I have stuck to the following problem of removing the duplcation of array
$result=array(2) { [0]=> object(stdClass)#489 (5) { ["id"]=> string(2) "64" ["block_from_id"]=> string(3) "117" ["block_to_id"]=> string(3) "329" ["block_status"]=> string(1) "0" ["username"]=> string(5) "pppoo" } [1]=> object(stdClass)#490 (5) { ["id"]=> string(2) "65" ["block_from_id"]=> string(3) "117" ["block_to_id"]=> string(3) "590" ["block_status"]=> string(1) "0" ["username"]=> string(3) "Pet" } }
$customerlist= array(7) { [0]=> object(stdClass)#491 (5) { ["customer_name"]=> string(4) "User" ["profile_image"]=> string(47) "http://pet.huarisnaque.com/pet/upload/90113.png" ["jid"]=> string(18) "user128#canopus-pc" ["customer_id"]=> string(3) "128" ["phone"]=> string(10) "4784784784" } [1]=> object(stdClass)#494 (5) { ["customer_name"]=> string(6) "Khatru" ["profile_image"]=> string(46) "http://smartpetmanagement.com/upload/61694.png" ["jid"]=> string(20) "khatru321#canopus-pc" ["customer_id"]=> string(3) "321" ["phone"]=> string(10) "9686838386" } [2]=> object(stdClass)#495 (5) { ["customer_name"]=> string(5) "pppoo" ["profile_image"]=> string(46) "http://smartpetmanagement.com/upload/35210.png" ["jid"]=> string(17) "yyy329#canopus-pc" ["customer_id"]=> string(3) "329" ["phone"]=> string(10) "9525538835" } [3]=> object(stdClass)#496 (5) { ["customer_name"]=> string(7) "Xitxitx" ["profile_image"]=> NULL ["jid"]=> string(21) "xitxitx330#canopus-pc" ["customer_id"]=> string(3) "330" ["phone"]=> string(10) "6535383535" } [4]=> object(stdClass)#497 (5) { ["customer_name"]=> string(25) "The Following Document yf" ["profile_image"]=> string(46) "http://smartpetmanagement.com/upload/13712.png" ["jid"]=> string(39) "the following document yf589#canopus-pc" ["customer_id"]=> string(3) "589" ["phone"]=> string(10) "9535383535" } [5]=> object(stdClass)#498 (5) { ["customer_name"]=> string(3) "Pet" ["profile_image"]=> NULL ["jid"]=> string(17) "pet590#canopus-pc" ["customer_id"]=> string(3) "590" ["phone"]=> string(10) "6560530537" } [6]=> object(stdClass)#499 (5) { ["customer_name"]=> string(10) "Sanjay Pra" ["profile_image"]=> NULL ["jid"]=> string(24) "sanjay pra599#canopus-pc" ["customer_id"]=> string(3) "599" ["phone"]=> string(10) "2828282822" } }
there are two arrays,we need to remove duplicate records from the array containing two elements from result having elements from the customerlist.
here is my approach
for($i=0;$i<count($customerslist);$i++)
{
for($j=0;$j<count($result);$i++)
{
// if($result[$j]->block_to_id==$customerslist[$i]->customer_id)
{
unset($customerslist[$i]);
}
echo $result[$j]->block_to_id."<br/>";
}
}
You can unset the values after you find the indexes that has the duplicate. Because if you unset it inside the for loop of checking your duplicates it will produce this error because the size/count of the array changes and the index does not match anymore:
Notice: Undefined offset: 2
So my solution is you can put the matching indexes on another array then unset them on another for loop shown bellow:
$result=array(
array('id' => 64,
"block_from_id" => 117,
"block_to_id" => 329,
"block_status" => 0,
"username" => "pppoo"),
array("id"=> 65,
"block_from_id"=> 117,
"block_to_id"=> 590,
"block_status"=> 0,
"username"=> "Pet"
)
);
$customerlist= array(
array("customer_name" => "User" ,"profile_image" => "http://pet.huarisnaque.com/pet/upload/90113.png" ,"jid" => "user128#canopus-pc" ,"customer_id" => "128" ,"phone" => "4784784784"),
array("customer_name" => "Khatru" ,"profile_image" => "http://smartpetmanagement.com/upload/61694.png" ,"jid" => "khatru321#canopus-pc" ,"customer_id" => "321" ,"phone" => "9686838386"),
array("customer_name" => "pppoo" ,"profile_image" => "http://smartpetmanagement.com/upload/35210.png" ,"jid" => "yyy329#canopus-pc" ,"customer_id" => "329" ,"phone" => "9525538835"),
array("customer_name" => "Xitxitx" ,"profile_image " => NULL ,"jid" => "xitxitx330#canopus-pc" ,"customer_id" => "330" ,"phone" => "6535383535"),
array("customer_name" => "The Following Document yf" ,"profile_image" => "http://smartpetmanagement.com/upload/13712.png" ,"jid" => "the following document yf589#canopus-pc" ,"customer_id" => "589" ,"phone" => "9535383535"),
array("customer_name" => "Pet" ,"profile_image " => NULL ,"jid" => "pet590#canopus-pc" ,"customer_id" => "590" ,"phone" => "6560530537"),
array("customer_name" => "Sanjay Pra" ,"profile_image " => NULL ,"jid" => "sanjay pra599#canopus-pc" ,"customer_id" => "599" ,"phone" => "2828282822")
);
echo "Before:". sizeof($customerlist) ."<br>";
print_r($customerlist);
echo "<br>";
echo "<br>";
$match = array();
for ($i=0; $i < sizeof($result) ; $i++) {
for ($ii=0; $ii < sizeof($customerlist) ; $ii++) {
if ($result[$i]['block_to_id'] == $customerlist[$ii]['customer_id']) {
$match[] = $ii;
echo " Match INDEX on result $i == customerlist $ii<br>";
}
}
}
for ($i=0; $i < sizeof($match) ; $i++) {
$ii = $match[$i];
unset($customerlist[$ii]);
}
echo "<br>";
echo "After: ". sizeof($customerlist) ."<br>";
print_r($customerlist);
OUTPUT:
Before:7
Array ( [0] => Array ( [customer_name] => User [profile_image] => http://pet.huarisnaque.com/pet/upload/90113.png [jid] => user128#canopus-pc [customer_id] => 128 [phone] => 4784784784 ) [1] => Array ( [customer_name] => Khatru [profile_image] => http://smartpetmanagement.com/upload/61694.png [jid] => khatru321#canopus-pc [customer_id] => 321 [phone] => 9686838386 ) [2] => Array ( [customer_name] => pppoo [profile_image] => http://smartpetmanagement.com/upload/35210.png [jid] => yyy329#canopus-pc [customer_id] => 329 [phone] => 9525538835 ) [3] => Array ( [customer_name] => Xitxitx [profile_image ] => [jid] => xitxitx330#canopus-pc [customer_id] => 330 [phone] => 6535383535 ) [4] => Array ( [customer_name] => The Following Document yf [profile_image] => http://smartpetmanagement.com/upload/13712.png [jid] => the following document yf589#canopus-pc [customer_id] => 589 [phone] => 9535383535 ) [5] => Array ( [customer_name] => Pet [profile_image ] => [jid] => pet590#canopus-pc [customer_id] => 590 [phone] => 6560530537 ) [6] => Array ( [customer_name] => Sanjay Pra [profile_image ] => [jid] => sanjay pra599#canopus-pc [customer_id] => 599 [phone] => 2828282822 ) )
Match INDEX on result 0 == customerlist 2
Match INDEX on result 1 == customerlist 5
After: 5
Array ( [0] => Array ( [customer_name] => User [profile_image] => http://pet.huarisnaque.com/pet/upload/90113.png [jid] => user128#canopus-pc [customer_id] => 128 [phone] => 4784784784 ) [1] => Array ( [customer_name] => Khatru [profile_image] => http://smartpetmanagement.com/upload/61694.png [jid] => khatru321#canopus-pc [customer_id] => 321 [phone] => 9686838386 ) [3] => Array ( [customer_name] => Xitxitx [profile_image ] => [jid] => xitxitx330#canopus-pc [customer_id] => 330 [phone] => 6535383535 ) [4] => Array ( [customer_name] => The Following Document yf [profile_image] => http://smartpetmanagement.com/upload/13712.png [jid] => the following document yf589#canopus-pc [customer_id] => 589 [phone] => 9535383535 ) [6] => Array ( [customer_name] => Sanjay Pra [profile_image ] => [jid] => sanjay pra599#canopus-pc [customer_id] => 599 [phone] => 2828282822 ) )
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"
}
}
}