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.
Related
$response is the data am getting when accessing an external API. i wanted to access the value of the key fileList. I tried like dd($result['serverResponse']['extensionServiceState']['payload']['fileList']) , but it returns error undefined index extensionServiceState . How can i achieve the required result?
$response = curl_exec($curl);
$result = json_decode($response, true);
//var_dump($result);
dd($result['serverResponse']['extensionServiceState']['payload']['fileList']);
before json decode
"""
{"resourceId":"abcd","sid":"123","serverResponse":{"extensionServiceState":[{"payload":{"fileList":[{"filename":"1.m3u8","sliceStartTime":16439789992},{"filename":"2.mp4","sliceStartTime":16439789992}],"onhold":false,"state":"exit"},"serviceName":"web_recorder_service"},{"payload":{"uploadingStatus":"uploaded"},"serviceName":"upload_service"}]}}
"""
after json decode
array:3 [
"resourceId" => "abcd"
"sid" => "123"
"serverResponse" => array:1 [
"extensionServiceState" => array:2 [
0 => array:2 [
"payload" => array:1 [
"uploadingStatus" => "uploaded"
]
"serviceName" => "upload_service"
]
1 => array:2 [
"payload" => array:3 [
"fileList" => array:2 [
0 => array:2 [
"filename" => "1.m3u8"
"sliceStartTime" => 1643986576940
]
1 => array:2 [
"filename" => "2.mp4"
"sliceStartTime" => 1643986576940
]
]
"onhold" => false
"state" => "exit"
]
"serviceName" => "web_recorder_service"
]
]
]
]
var_dump($$result) after json decode shows the below data
array(3) {
["resourceId"]=>
string(4)
"abcd"
["sid"]=>
string(3) "123"
["serverResponse"]=>
array(1) {
["extensionServiceState"]=>
array(2) {
[0]=>
array(2) {
["payload"]=>
array(1) {
["uploadingStatus"]=>
string(8) "uploaded"
}
["serviceName"]=>
string(14) "upload_service"
}
[1]=>
array(2) {
["payload"]=>
array(3) {
["fileList"]=>
array(2) {
[0]=>
array(2) {
["filename"]=>
string(45) "1.m3u8"
["sliceStartTime"]=>
int(16439789992)
}
[1]=>
array(2) {
["filename"]=>
string(46) "2.mp4"
["sliceStartTime"]=>
int(16439789992)
}
}
["onhold"]=>
bool(false)
["state"]=>
string(4) "exit"
}
["serviceName"]=>
string(20) "web_recorder_service"
}
}
}
}
Either you pasted the wrong content or json_decode is lying to you. You asked for an associative array from json_decode (by passing the second argument as true). The result of that should actually look like this:
Array
(
[resourceId] => abcd
[sid] => 123
[serverResponse] => Array
(
[extensionServiceState] => Array
(
[0] => Array
(
[payload] => Array
(
[fileList] => Array
(
[0] => Array
(
[filename] => 1.m3u8
[sliceStartTime] => 16439789992
)
[1] => Array
(
[filename] => 2.mp4
[sliceStartTime] => 16439789992
)
)
[onhold] =>
[state] => exit
)
[serviceName] => web_recorder_service
)
[1] => Array
(
[payload] => Array
(
[uploadingStatus] => uploaded
)
[serviceName] => upload_service
)
)
)
)
So therefore, to get what you want you would need to do this:
dd($result['serverResponse']['extensionServiceState'][0]['payload']['fileList']);
But your data contains multiple items inside extensionServiceState so you may want to loop over those.
I have an array
{
Array
(
[0] => Array
(
[filterType] => checkbox
[sfid] => a1d1I000000jrmwQAA
[name] => Publication Year
[specValues] => Array
(
[count] => 3
[value] => 1953
)
)
[1] => Array
(
[filterType] => checkbox
[sfid] => a1d1I000000jrmwQAA
[name] => Publication Year
[specValues] => Array
(
[count] => 1
[value] => 1954
)
)
)
}
But I want to merge array if sfid is same i.e i want result
{
Array
(
[0] => Array
(
[filterType] => checkbox
[sfid] => a1d1I000000jrmwQAA
[name] => Publication Year
[specValues] => Array(
[0] =>array(
[count] => 3
[value] => 1953
)
[1] =>array(
[count] => 1
[value] => 1954
)
)
)
)
}
You just need to iterate your array and add the row to the output if it is not in there already (you can use sfid as keys) and add specValues to the correct row.
<?php
$input = [
[
'filterType' => 'checkbox',
'sfid' => 'a1d1I000000jrmwQAA',
'name' => 'Publication Year',
'specValues' => [
'count' => 3,
'value' => 1953,
],
],
[
'filterType' => 'checkbox',
'sfid' => 'a1d1I000000jrmwQAA',
'name' => 'Publication Year',
'specValues' => [
'count' => 1,
'value' => 1954,
],
],
];
$data = [];
foreach ($input as $row) {
if (!isset($data[$row['sfid']])) {
$data[$row['sfid']] = [
'filterType' => $row['filterType'],
'sfid' => $row['sfid'],
'name' => $row['name'],
'specValues' => [],
];
}
$data[$row['sfid']]['specValues'][] = $row['specValues'];
}
var_dump(array_values($data));
Output:
array(1) {
[0]=>
array(4) {
["filterType"]=>
string(8) "checkbox"
["sfid"]=>
string(18) "a1d1I000000jrmwQAA"
["name"]=>
string(16) "Publication Year"
["specValues"]=>
array(2) {
[0]=>
array(2) {
["count"]=>
int(3)
["value"]=>
int(1953)
}
[1]=>
array(2) {
["count"]=>
int(1)
["value"]=>
int(1954)
}
}
}
}
I assume that if sfid is the same, then also filterType and name is the same.
I was working on Facebook API.
I have got this array:
["data"] => Array(31) {
[0] => Array(6) {
["id"] => String(13) "6003110325672"
["name"] => String(17) "Cristiano Ronaldo"
["audience_size"] => Integer 122006620
["path"] => Array(3) {
[0] => String(9) "Interessi"
[1] => String(20) "Interessi aggiuntivi"
[2] => String(17) "Cristiano Ronaldo"
}
["description"] => NULL
["topic"] => String(6) "People"
}
[1] => Array(6) {
["id"] => String(13) "6003114817426"
["name"] => String(10) "Ronaldinho"
["audience_size"] => Integer 17910990
["path"] => Array(3) {
[0] => String(9) "Interessi"
[1] => String(20) "Interessi aggiuntivi"
[2] => String(10) "Ronaldinho"
}
["description"] => NULL
["topic"] => String(6) "People"
}
which goes on with additional indexes. Then I've got this one:
["data"] => Array(45) {
[0] => Array(11) {
["id"] => String(13) "6003129962717"
["name"] => String(16) "Zinédine Zidane"
["type"] => NULL
["path"] => NULL
["description"] => NULL
["source"] => NULL
["partner"] => NULL
["audience_size"] => Integer 14137830
["country"] => NULL
["country_access"] => NULL
["topic"] => NULL
}
[1] => Array(11) {
["id"] => String(13) "6003115921142"
["name"] => String(13) "Thierry Henry"
["type"] => NULL
["path"] => NULL
["description"] => NULL
["source"] => NULL
["partner"] => NULL
["audience_size"] => Integer 2601710
["country"] => NULL
["country_access"] => NULL
["topic"] => NULL
}
[2] => Array(11) {
["id"] => String(13) "6003114817426"
["name"] => String(10) "Ronaldinho"
["type"] => NULL
["path"] => NULL
["description"] => NULL
["source"] => NULL
["partner"] => NULL
["audience_size"] => Integer 17910990
["country"] => NULL
["country_access"] => NULL
["topic"] => NULL
}
which as the first one, goes on with additional indexes.
First I tried to merge the arrays using array_merge(), but it didn't work, because I lost part of data inside of them. So I'd like to merge them correctly. Then, I noticed that in the second one I have "Ronaldinho" element as a duplicate (2nd array index 2 and 1st array index 1). I tried almost everything to figure out how to remove either the first or the second in the merged one, but PHP replies me everytime with errors and warnings.
My goal would be having both arrays in one, removing the duplicates, based on the 'name' element.
I thank whoever could help me, i'm stuck.
There are various ways to do this. One possibility is to use the ids from the first array to filter the second array before merging.
Get the ids:
$ids = array_flip(array_column($first, 'id'));
Create a filter:
$distinct = function($item) use ($ids) { return !isset($ids[$item['id']]); };
Filter and merge:
$result = array_merge($first, array_filter($second, $distinct));
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"
}
}
}
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"
}
}