How to get values out of a nested array in PHP [duplicate] - php

This question already has answers here:
Using array_map vs array_column and array_combine in php [closed]
(1 answer)
Generate an associative array from an array of rows using one column as keys and another column as values
(3 answers)
Closed 9 months ago.
returning to php after a VERY long break and need some help. So my function is returning the following array. I now want to get the 'id' and 'name' and return them in their own array where name is the key and id is the value. How would I go about it?
object(stdClass)#11 (1) {
["data"]=>
array(5) {
[0]=>
object(stdClass)#1 (3) {
["id"]=>
string(36) "7fd134a2-5f80-4d07-8a1a-a54c48f7e71b"
["type"]=>
string(13) "userattribute"
["attributes"]=>
object(stdClass)#2 (1) {
["name"]=>
string(10) "Test1Test1"
}
}
[1]=>
object(stdClass)#3 (3) {
["id"]=>
string(36) "dcdd87d5-1ff3-4fd9-8a75-7e20fe8f19d7"
["type"]=>
string(13) "userattribute"
["attributes"]=>
object(stdClass)#4 (1) {
["name"]=>
string(10) "Test1Test2"
}
}
[2]=>
object(stdClass)#5 (3) {
["id"]=>
string(36) "b167b703-a63f-4548-9104-43a436871f45"
["type"]=>
string(13) "userattribute"
["attributes"]=>
object(stdClass)#6 (1) {
["name"]=>
string(10) "Test1Test3"
}
}
[3]=>
object(stdClass)#7 (3) {
["id"]=>
string(36) "6a17046b-b665-493f-83be-35e0a9129c49"
["type"]=>
string(13) "userattribute"
["attributes"]=>
object(stdClass)#8 (1) {
["name"]=>
string(10) "Test1Test4"
}
}
[4]=>
object(stdClass)#9 (3) {
["id"]=>
string(36) "4704cf6f-1c8b-432e-88df-f6865c8690d5"
["type"]=>
string(13) "userattribute"
["attributes"]=>
object(stdClass)#10 (1) {
["name"]=>
string(10) "Test1Test5"
}
}
}
}

Use array_column() to extract each column, and array_combine() to merge them so one is the key and the other is the value of an associative array.
$result = array_combine(array_column($object->data, 'id'), array_column($object->data, 'name'));
where $object is the variable holding the data you posted.

Related

array value inside array

Ok I did some searching and everything I saw involved loops, but I don't think it would really require on. I'm trying to get the 'href' value which appears to be within an array in the initial array:
Trying to access data in an array that has an embedded array how do I get [actions][href] ?:
["domains"]=>
array(1) {
[0]=>
array(5) {
["resource"]=>
string(96) "xxxx"
["name"]=>
string(34) "yyyy"
["type"]=>
string(9) "Blacklist"
["count"]=>
int(2672)
["actions"]=>
array(1) {
[0]=>
array(1) {
["download"]=>
array(2) {
["href"]=>
string(105) "WANT THIS VALUE"
["method"]=>
string(3) "GET"

Accessing form data form JSON object converted to associative array in PHP [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 3 years ago.
I'm sending the HTML FORM data with AJAX to PHP script and I'm converting the data to associative array like this:
$json = json_decode($request->get('json'),true);
so, when I'm dumping the variable I get this array:
array(11) {
[0]=>
array(2) {
["name"]=>
string(21) "form[pickupDate][day]"
["value"]=>
string(1) "4"
}
[1]=>
array(2) {
["name"]=>
string(23) "form[pickupDate][month]"
["value"]=>
string(1) "1"
}
[2]=>
array(2) {
["name"]=>
string(22) "form[pickupDate][year]"
["value"]=>
string(4) "2016"
}
[3]=>
array(2) {
["name"]=>
string(22) "form[pickupTime][hour]"
["value"]=>
string(1) "0"
}
[4]=>
array(2) {
["name"]=>
string(21) "form[returnDate][day]"
["value"]=>
string(1) "1"
}
[5]=>
array(2) {
["name"]=>
string(23) "form[returnDate][month]"
["value"]=>
string(1) "1"
}
[6]=>
array(2) {
["name"]=>
string(22) "form[returnDate][year]"
["value"]=>
string(4) "2016"
}
[7]=>
array(2) {
["name"]=>
string(22) "form[returnTime][hour]"
["value"]=>
string(1) "0"
}
[8]=>
array(2) {
["name"]=>
string(19) "form[pickupAddress]"
["value"]=>
string(0) ""
}
[9]=>
array(2) {
["name"]=>
string(12) "form[agency]"
["value"]=>
string(1) "1"
}
[10]=>
array(2) {
["name"]=>
string(12) "form[_token]"
["value"]=>
string(43) "9dh6ghpMv5K9LUdSLvh6y2NOzqTzUrOfVriL8C63Ybs"
}
}
how can I get the value of form[pickupDate][day] without loop?
I was able to do it like this:
echo array_column($json, null, 'name')['form[pickupDate][day]']['value'];

PHP, stdClass, select elements contains specifed element

I have an stClass object like this:
object(stdClass)#2 (6) {
[0]=>
object(stdClass)#44 (2) {
["uid"]=>
int(3232)
["type"]=>
string(7) "sibling"
}
[1]=>
object(stdClass)#43 (2) {
["uid"]=>
int(32323)
["type"]=>
string(7) "sibling"
}
[2]=>
object(stdClass)#42 (2) {
["uid"]=>
int(3213)
["type"]=>
string(10) "grandchild"
}
[3]=>
object(stdClass)#41 (3) {
["uid"]=>
int(-680411188)
["type"]=>
string(6) "parent"
}
[4]=>
object(stdClass)#40 (3) {
["uid"]=>
int(-580189276)
["type"]=>
string(6) "parent"
}
[5]=>
object(stdClass)#39 (2) {
["uid"]=>
int(3213)
["type"]=>
string(7) "sibling"
}
}
How can I get elements with specified value of type element?
For example, if I select "parent", I wanna get this:
object(stdClass)#2 (6) {
[3]=>
object(stdClass)#41 (3) {
["uid"]=>
int(-680411188)
["type"]=>
string(6) "parent"
}
[4]=>
object(stdClass)#40 (3) {
["uid"]=>
int(-580189276)
["type"]=>
string(6) "parent"
}
}
I know, how to write it with "foreach" and "if", but I hope that there is another way. Thanks
Your outer object is in fact, an array in disguise. You can convert it to a real array by typecasting:
$arr = (array)$obj;
Then you can use:
$filtered = array_filter(
$arr,
function($item) {
return $item->type == 'parent';
}
);
to get an array that contains only the objects you need.

combine two array without foreach

I am making a mailing system, and my input is array of arrays, i need to combine them to one, i already aggregated based on the email.
input example:
array(2) {
[0]=>
array(15) {
["enabled"]=>
string(1) "1"
["file_size_bytes"]=>
string(6) "200122"
["email"]=>
string(21) "jon#gmail.com"
["content"]=>
string(34) "{"Notice":827,"co":3241,"Co":1555}"
}
[1]=>
array(15) {
["enabled"]=>
string(1) "1"
["file_size_bytes"]=>
string(6) "592024"
["email"]=>
string(21) "jon#gmail.com"
["content"]=>
string(97) "{"Co":388,"co":5564,"xml":2982,"CO":6,"Warning":1957,"warning":42,"Notice":13,"cO":9,"Connect":6}"
}
}
array(1) {
[0]=>
array(15) {
["enabled"]=>
string(1) "1"
["file_size_bytes"]=>
string(5) "19116"
["email"]=>
string(22) "kelly#gmail.com"
["content"]=>
string(8) "{"co":1}"
}
}
input array to the mail function should look like:
array(1) {
[0]=>
array(15) {
["enabled"]=>
string(1) "1"
["file_size_bytes"]=>
string(6) "200122"
["email"]=>
string(21) "jon#gmail.com"
["content"]=>
string(34) "{"Notice":827,"co":3241,"Co":1555}"
["enabled"]=>
string(1) "1"
["file_size_bytes"]=>
string(6) "592024"
["email"]=>
string(21) "jon#gmail.com"
["content"]=>
string(97) " {"Co":388,"co":5564,"xml":2982,"CO":6,"Warning":1957,"warning":42,"Notice":13,"cO":9,"Conne ct":6}"
}
}
array(1) {
[0]=>
array(15) {
["enabled"]=>
string(1) "1"
["file_size_bytes"]=>
string(5) "19116"
["email"]=>
string(22) "kelly#gmail.com"
["content"]=>
string(8) "{"co":1}"
}
}
its basically spouse to combine the two arrays that are in the same array.
how can i do that? thanks :)
The required outcome is not possible since you have duplicate keys in the array which won't be possible in PHP.
e.g.
["content"] => string(34) "{"Notice":827,"co":3241,"Co":1555}"
will be replaced by
["content"] => string(97) "{"Co":388,"co":5564,"xml":2982,"CO":6,"Warning":1957,"warning":42,"Notice":13,"cO":9,"Connect":6}"
You can do this simply using the array_merge function.
In their answer, Maarten suggests this is not possible because duplicate keys would be overwritten. However this only occurs when the keys aren't numeric. In your example above the keys of the first array are 0 and 1 and the second just 0. All numeric.
So all you need to do is:
array_merge($array1,$array2);

Merging two multidimensional associative arrays [duplicate]

This question already has answers here:
Merge row data from multiple arrays
(6 answers)
Closed last month.
I'm chasing my tail trying to combine the results of two different queries to output in a template.
I'm trying to merge the corresponding sub-arrays in model_data and entry_data to get desired_result. I will then iterate over desired_result and print values into the template.
Any assistance is greatly appreciated.
model_data
array(2) {
[0]=>
array(2) {
["entry_id"]=> string(3) "192"
["field_id_49"]=> string(10) "Model Name"
}
[1]=>
array(2) {
["entry_id"]=> string(3) "193"
["field_id_49"]=> string(5) "MN123"
}
}
entry_data
array(2) {
[0]=>
array(2) {
["uri"]=> string(24) "/products/product-title/"
["title"]=> string(13) "Product Title"
}
[1]=>
array(2) {
["uri"]=> string(22) "/products/lorem-ipsum/"
["title"]=> string(11) "Lorem Ipsum"
}
}
desired_result
array(2) {
[0]=>
array(4) {
["entry_id"]=> string(3) "192"
["field_id_49"]=> string(10) "Model Name"
["uri"]=> string(24) "/products/product-title/"
["title"]=> string(13) "Product Title"
}
[1]=>
array(4) {
["entry_id"]=> string(3) "193"
["field_id_49"]=> string(5) "MN123"
["uri"]=> string(22) "/products/lorem-ipsum/"
["title"]=> string(11) "Lorem Ipsum"
}
}
foreach($model_data as $key => $value){
$result[$key] = array_merge($entry_data[$key], $model_data[$key]);
}
You can use array_replace_recursive function to merge these arrays.
Below is a example:
$desired_result = array_replace_recursive($model_data, $entry_data);
var_dump($desired_result);

Categories