array value inside array - php

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"

Related

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

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.

PHP_XLSwriter array output

I'm working with a bit of php code called PHP_XLSWriter. It accepts an input, then outputs it to an Excel sheet.
Their example.php page does exactly what I want, and in it, there's a bit of code that determines what specifically gets exported:
$data1 = array(
array('2003','1','-50.5','2010-01-01 23:00:00'),
array('2003','=B2', '23.5','2010-01-01 00:00:00'),
);
I haven't written arrays like this exactly in the past, but I've passed my own array into this page through AJAX from a separate page. I'm not sure if it's important how I did this, but I'm happy to paste in that code if it is.
When I try to execute the page using MY array, the page fails and my console is full of intelligible text.
I can't figure out why this is as the var_dump of our two arrays are exactly the same.
the original var_dump($data1);
array(2) {
[0]=>
array(4) {
[0]=>
string(4) "2003"
[1]=>
string(1) "1"
[2]=>
string(5) "-50.5"
[3]=>
string(19) "2010-01-01 23:00:00"
}
[1]=>
array(4) {
[0]=>
string(4) "2003"
[1]=>
string(3) "=B2"
[2]=>
string(4) "23.5"
[3]=>
string(19) "2010-01-01 00:00:00"
}
}
the var_dump() when I set $data1 equal to the array I've imported:
array(2) {
[0]=>
array(4) {
[0]=>
string(6) "blah"
[1]=>
string(12) "two"
[2]=>
string(6) "0.0160"
[3]=>
string(6) "0.0173"
}
[1]=>
array(4) {
[0]=>
string(3) "blah"
[1]=>
string(14) "three"
[2]=>
string(6) "0.0085"
[3]=>
string(6) "0.0095"
}
}
and echo gettype($data1); outputs array for both of the strings.
What might I be doing to cause this to break? I'm positive it has something to do with my array, as all else held equal, the code works.

Php Put into new array one row of another multidimentional associative array

I have multidimensional associative array returned by a MYSQL table.
array { [0]=> array
{
["id"]=> string(8) "130"
["name"]=> string(7) "mike"
["nickname"]=> string(7) "raze"
}
[1]=> array
{
["id"]=> string(8) "120"
["name"]=> string(7) "jimmy"
["nickname"]=> string(5) "killer"
}
}
I would extract one row into another multidimensional associative array with the same structure.
After this, my new array should be:
array { [0]=> array
{
["id"]=> string(8) "120"
["name"]=> string(7) "jimmy"
["nickname"]=> string(5) "killer"
}
}
Anyone can help me ?
Thank you so much.

PHP ARRAY get index of item for certain value

I have these 2 arrays $fonts['google'] and $data['value'] with the following content:
var_dump ($fonts['google']) outputs
array(4) {
[0]=> array(3) { ["family"]=> string(7) "ABeeZee" ["variants"]=> array(2) { [0]=> string(7) "regular" [1]=> string(6) "italic" } ["subsets"]=> array(1) { [0]=> string(5) "latin" } }
[1]=> array(3) { ["family"]=> string(4) "Abel" ["variants"]=> array(1) { [0]=> string(7) "regular" } ["subsets"]=> array(1) { [0]=> string(5) "latin" } }
[2]=> array(3) { ["family"]=> string(13) "Abril Fatface" ["variants"]=> array(1) { [0]=> string(7) "regular" } ["subsets"]=> array(2) { [0]=> string(5) "latin" [1]=> string(9) "latin-ext" } }
[3]=> array(3) { ["family"]=> string(8) "Aclonica" ["variants"]=> array(1) { [0]=> string(7) "regular" } ["subsets"]=> array(1) { [0]=> string(5) "latin" } }
}
var_dump ($data['value']) outputs
array(4) {
["size"]=> int(17)
["family"]=> string(3) "Exo"
["style"]=> string(3) "200"
["subsets"]=> string(5) "latin"
}
Now I get the $data['value']['family'] = 'Abel' from my database.
Questions:
How can I get the ['variants'] for the given $data['value']['family'] value?
How can I get the index in $fonts['google'] for the sub-array where the $data['value']['family'] value is?
PHP supports Associative Arrays which let you use a (string) key rather than a numeric index for each element. These arrays are akin to javascript objects, Objective-C dictionaries, java HashMaps, etc.
That makes scenarios like this easy. Do you have control over building the original data array? If you can refactor your storage, set up the arrays like this:
$fonts['google'] = [
["ABeeZee"] => [
["variants"]=>["regular", "italic"],
["subsets"]=>["latin"]
],
["Abel"] => [
["variants"]=>["regular"],
["subsets"]=>["latin"]
],
["Abril Fatface"] => [
["variants"]=>["regular"],
["subsets"]=>["latin", "latin-ext"]
],
["Aclonica"] => [
["variants"]=>["regular"],
["subsets"]=>["latin"]
]
]
extra credit: if you have the original data as in the post, you could convert it:
$newArray = array(); // or just [] in PHP >= 5.3 I believe
foreach($fonts['google'] as $index=>$fontArray) {
$newArray[$fontArray['family']] = $fontArray;
// this leaves a redundant copy of the family name in the subarray
unset $newArray[$fontArray['family']]['family']; // if you want to remove the extra copy
}
Then it becomes trivial. Given a font family name, you just access $fonts['google'][$fontFamilyName] (or $newArray[$fontFamilyName]) using the family name as the array index.

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);

Categories