$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.
Related
i have an array of arrays like this one
array(4) {
[0] => array(2) {
["option"] => string(5) "64310"
["choice"] => string(6) "221577"
}
[1] => array(2) {
["option"] => string(5) "64310"
["choice"] => string(6) "221578"
}
[2] => array(2) {
["option"] => string(5) "64305"
["choice"] => string(6) "221538"
}
}
i want to obtain a result like this one
array(2) {
[0] => array(2) {
["option"] => string(5) "64310"
["choices"] => array(2){
["choice"] => string(6) "221577"
["choice"] => string(6) "221578"
}
}
}
how can i proceed, thank you in advance
Something like this will help you achieve the desired result;
<?php
$data = [
[
'option' => '64310',
'choice' => '221577'
],
[
'option' => '64310',
'choice' => '221578'
],
[
'option' => '64305',
'choice' => '221538'
]
];
$res = [];
foreach($data as $d) {
// Check if we've already got this option
// Note the '&' --> Check link below
foreach($res as &$r) {
if (isset($r['option']) && $r['option'] === $d['option']) {
// Add to 'choices'
$r['choices'][] = $d['choice'];
// Skip the rest of both foreach statements
continue 2;
}
}
// Not found, create
$res[] = [
'option' => $d['option'],
'choices' => [ $d['choice'] ],
];
};
print_r($res);
& --> PHP "&" operator
Array
(
[0] => Array
(
[option] => 64310
[choices] => Array
(
[0] => 221577
[1] => 221578
)
)
[1] => Array
(
[option] => 64305
[choices] => Array
(
[0] => 221538
)
)
)
Try online!
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
)
)
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.
This question already has answers here:
Merge two 2d arrays by shared column value
(6 answers)
Closed 2 months ago.
I have this array that I have to replace the values according to their id. below is the Original Array:
[
0 => [
"id" => "70"
"color" => "red"
]
1 => [
"id" => "65"
"color" => "blue"
]
2 => [
"id" => "66"
"color" => "black"
]
3 => [
"id" => "73"
"color" => "red"
]
]
And this is the array that I need to insert and replace the array that has the same id in the original array:
0 => [
"id" => "65"
"color" => "white"
]
1 => [
"id" => "66"
"color" => "gold"
]
]
What I am trying to achieve is something like this:
[
0 => [
"id" => "70"
"color" => "red"
]
1 => [
"id" => "65"
"color" => "white"
]
2 => [
"id" => "66"
"color" => "gold"
]
3 => [
"id" => "73"
"color" => "red"
]
]
Simple solution with array_column and array_walk functions:
// $arr1 is the original array
// $arr2 is the replacing array
$colours = array_column($arr2, "color", "id");
array_walk($arr1, function(&$v) use($colours){
if (array_key_exists($v["id"],$colours)) {
$v["color"] = $colours[$v["id"]];
}
});
print_r($arr1);
The output:
Array
(
[0] => Array
(
[id] => 70
[color] => red
)
[1] => Array
(
[id] => 65
[color] => white
)
[2] => Array
(
[id] => 66
[color] => gold
)
[3] => Array
(
[id] => 73
[color] => red
)
)
http://php.net/manual/ru/function.array-column.php
http://php.net/manual/ru/function.array-walk.php
Try this code:
<?php
$original = [
0 => [
"id" => "70",
"color" => "red" ,
],
1 => [
"id" => "65",
"color" => "blue",
],
2 => [
"id" => "66",
"color" => "black",
],
3 => [
"id" => "73",
"color" => "red",
]
];
$toReplace = [0 => [
"id" => "65",
"color" => "white" ,
],
1 => [
"id" => "66",
"color" => "gold",
]
];
function getColorByKey($key, $toReplace)
{
$result = null;
foreach($toReplace as $k => $value)
{
if($value['id'] == $key)
$result = $value['color'];
}
return $result;
}
foreach($original as $key => $value)
{
$newColor = getColorByKey($value['id'], $toReplace);
$original[$key]['color'] = $newColor !== null ? $newColor : $original[$key]['color'];
}
var_dump($original);
Output of var_dump:
array(4) {
[0]=>
array(2) {
["id"]=>
string(2) "70"
["color"]=>
string(3) "red"
}
[1]=>
array(2) {
["id"]=>
string(2) "65"
["color"]=>
string(5) "white"
}
[2]=>
array(2) {
["id"]=>
string(2) "66"
["color"]=>
string(4) "gold"
}
[3]=>
array(2) {
["id"]=>
string(2) "73"
["color"]=>
string(3) "red"
}
}
There is a function getColorByKey($key, $toReplace) which is used in foreach loop, where $key is id index, and $toReplace is your second array.
// $array1 = original array
// $array2 = second array
foreach ($array1 as $key1 => $value)
{
$new[$value['id']] = $key1;
}
foreach ($array2 as $value)
{
if (array_key_exists($value['id'], $new))
{
$key2 = $new[$value['id']];
$array1[$key2]['color'] = $value['color'];
}
else
{
$array1[] = array('id' => $value['id'], 'color' => $value['color']);
// if this color not present, then it adds this to the original array
}
}
echo '<pre>'; print_r($array1);
Output:
Array
(
[0] => Array
(
[id] => 70
[color] => red
)
[1] => Array
(
[id] => 65
[color] => white
)
[2] => Array
(
[id] => 66
[color] => gold
)
[3] => Array
(
[id] => 73
[color] => red
)
)
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"
}
}
}