Find Array With 'SELECTED' Value Inside Multidimensional Array - php

I've got a very large JSON file that I've converted into an array. The code you see below is the result of
var_dump($array['talents'])
(for those wondering, yes, this is me attempting to use Blizzard Entertainment's API)
Maybe I just missed that day at PHP school, but I just can't seem to remember or figure out how to get the values inside these deeper arrays. And perhaps more importantly, pay attention to the two 'spec' fields - ONE of them has a value of 'SELECTED: BOOLEAN TRUE', and the other does not. Basically, I need the NAME field from the array that has that SELECTED value.
To be honest, much as I love working in PHP, this multi-level thinking is a tad beyond me at the moment, I need some more experience. In the meantime, could anyone give me some tips, pointers, or just straight-up answers as to how I can get the value I need?
Thanks a bunch for your time!
array (size=2)
0 =>
array (size=6)
'talents' =>
array (size=6)
0 =>
array (size=3)
...
1 =>
array (size=3)
...
2 =>
array (size=3)
...
3 =>
array (size=3)
...
4 =>
array (size=3)
...
5 =>
array (size=3)
...
'glyphs' =>
array (size=2)
'major' =>
array (size=0)
...
'minor' =>
array (size=1)
...
'spec' =>
array (size=6)
'name' => string 'Frost' (length=5)
'role' => string 'DPS' (length=3)
'backgroundImage' => string 'bg-mage-frost' (length=13)
'icon' => string 'spell_frost_frostbolt02' (length=23)
'description' => string 'Freezes enemies in their tracks and shatters them with Frost magic.' (length=67)
'order' => int 2
'calcTalent' => string '120021.' (length=7)
'calcSpec' => string 'b' (length=1)
'calcGlyph' => string 'O' (length=1)
1 =>
array (size=7)
'selected' => boolean true
'talents' =>
array (size=6)
0 =>
array (size=3)
...
1 =>
array (size=3)
...
2 =>
array (size=3)
...
3 =>
array (size=3)
...
4 =>
array (size=3)
...
5 =>
array (size=3)
...
'glyphs' =>
array (size=2)
'major' =>
array (size=0)
...
'minor' =>
array (size=0)
...
'spec' =>
array (size=6)
'name' => string 'Fire' (length=4)
'role' => string 'DPS' (length=3)
'backgroundImage' => string 'bg-mage-fire' (length=12)
'icon' => string 'spell_fire_firebolt02' (length=21)
'description' => string 'Ignite enemies with balls of fire and combustive flames.' (length=56)
'order' => int 1
'calcTalent' => string '122012.' (length=7)
'calcSpec' => string 'Z' (length=1)
'calcGlyph' => string '' (length=0)

should be
print_r($array['talents'][0]);
print_r($array['glyphs']['major']);

Related

Replacing array keys with values within the same array

I need to replace the keys of my array with keys within the same array in PHP.
My array looks like this at the moment :
array (size=7)
0 =>
array (size=2)
'id' => int 978
'nbUnitsSold' => string '33.00000' (length=8)
1 =>
array (size=2)
'id' => int 979
'nbUnitsSold' => string '9.00000' (length=7)
2 =>
array (size=2)
'id' => int 980
'nbUnitsSold' => string '4.00000' (length=7)
3 =>
array (size=2)
'id' => int 981
'nbUnitsSold' => string '237.00000' (length=9)
4 =>
array (size=2)
'id' => int 983
'nbUnitsSold' => string '5.00000' (length=7)
5 =>
array (size=2)
'id' => int 984
'nbUnitsSold' => string '19.00000' (length=8)
6 =>
array (size=2)
'id' => int 991
'nbUnitsSold' => string '2.00000' (length=7)
I want it to look like this :
array (size=7)
978 =>
array (size=1)
'id' => int 978
'nbUnitsSold' => string '33.00000' (length=8)
979 =>
array (size=1)
'id' => int 979
'nbUnitsSold' => string '9.00000' (length=7)
980 =>
array (size=1)
'id' => int 980
'nbUnitsSold' => string '4.00000' (length=7)
981 =>
array (size=1)
'id' => int 981
'nbUnitsSold' => string '237.00000' (length=9)
983 =>
array (size=1)
'id' => int 983
'nbUnitsSold' => string '5.00000' (length=7)
984 =>
array (size=1)
'id' => int 984
'nbUnitsSold' => string '19.00000' (length=8)
991 =>
array (size=1)
'id' => int 991
'nbUnitsSold' => string '2.00000' (length=7)
Is there any function in PHP that could help without looping into the whole array ?
Thank you !
Just use array_column() with the third parameter as the column you want as the index...
$indexedArray = array_column($input,null,"id");

PHP. array_column() analogue for SimpleXMLElement object

object(SimpleXMLElement)[803]
public 'row' =>
array (size=13)
0 =>
object(SimpleXMLElement)[797]
public '#attributes' =>
array (size=4)
'codeonimage' => string '01' (length=2)
'name' => string 'Крышка' (length=12)
'oem' => string '13711251885' (length=11)
'ssd' => string '$HgsQRnNPF0d$' (length=174)
public 'attribute' =>
array (size=2)
0 =>
object(SimpleXMLElement)[813]
public '#attributes' =>
array (size=3)
'key' => string 'amount' (length=6)
'name' => string 'Количество' (length=20)
'value' => string '1' (length=1)
1 =>
object(SimpleXMLElement)[814]
public '#attributes' =>
array (size=3)
'key' => string 'end_of_production' (length=17)
'name' => string 'end_of_production' (length=17)
'value' => string 'Не производтся с: 19871116' (length=40)
...
I'm trying to take all the 'oem' values from each SimpleXML element like:
array_column($simpleXMLObject->row, 'oem');
... and of course I get an error:
array_column() expects parameter 1 to be array, object given
There is another option with a full search. But maybe there is some more pretty way to do this?

MongoDb get only value but not key using php

i am using mongodb and my query returns only a key value
the following is the format of the var_dump
<pre>array (size=471)
0 =>
array (size=1)
'sno' => string '162230' (length=6)
1 =>
array (size=1)
'sno' => string '165333' (length=6)
2 =>
array (size=1)
'sno' => string '181312' (length=6)
3 =>
array (size=1)
'sno' => string '181313' (length=6)
4 =>
array (size=1)
'sno' => string '181314' (length=6)
5 =>
array (size=1)
'sno' => string '181315' (length=6)
6 =>
array (size=1)
'sno' => string '181316' (length=6)
7 =>
array (size=1)
'sno' => string '181317' (length=6)
8 =>
array (size=1)
'sno' => string '181318' (length=6)
9 =>
array (size=1)
'sno' => string '181319' (length=6)
10 =>
array (size=1)
'sno' => string '181320' (length=6)</pre>
I do not want key . all i want is a array of values without a loop.
Solved it.
array_column($arr,"sno")

All data in a table with relationships, empty arrays

I have the Post, Category and Tag models with their relationships and working well in other inquiries.
If I get the categories and tags of a post specifically, the data returns correctly me.
$post = Post::with('categorias', 'etiquetas')->find($id)->toArray();
With Post::has only shows data Posts that do have categories, at least I understand it, so that does not help me.
$posts = Post::has('category')->get()->toArray();
But when I try to get all posts with their corresponding categories and tags for each post, I get a data array with an array post more empty categories and labels. As I can make the array comes with its data.
$posts = Post::with('categorias', 'etiquetas')->get()->toArray();
1 =>
array (size=11)
'id' => string '2' (length=1)
'titulo' => string 'A title once again' (length=18)
'subtitulo' => string '' (length=0)
'contenido' => string 'And the post body follows.' (length=26)
'created_at' => string '2015-04-09 13:19:12' (length=19)
'updated_at' => string '2015-05-04 13:44:25' (length=19)
'user_id' => null
'publicacion' => string '0000-00-00 00:00:00' (length=19)
'activo' => string '0' (length=1)
'categorias' =>
array (size=5)
0 =>
array (size=3)
...
1 =>
array (size=3)
...
2 =>
array (size=3)
...
3 =>
array (size=3)
...
4 =>
array (size=3)
...
'etiquetas' =>
array (size=0)
empty
I auto answer my question.
The query works correctly:
$posts = Post::with('categorias', 'etiquetas')->get()->toArray();
In laravel 4, the dumper does not go so far as to display data relationships, so you have to get there otherwise.
dd($posts[1]['categorias']);
array (size=5)
0 =>
array (size=3)
'id' => string '7' (length=1)
'nombre' => string 'CATEGORIA1' (length=13)
'pivot' =>
array (size=2)
'post_id' => string '2' (length=1)
'categoria_id' => string '7' (length=1)
1 =>
array (size=3)
'id' => string '8' (length=1)
'nombre' => string 'CATEGORIA2' (length=17)
'pivot' =>
array (size=2)
'post_id' => string '2' (length=1)
'categoria_id' => string '8' (length=1)
2...

Order by value of array multidimensional

I have array like this
array (size=6)
0 =>
array (size=1)
0 =>
array (size=5)
'document_id' => string '231' (length=3)
'document_title' => string 'Renstra 2014-2018 Distamben COVER' (length=33)
'document_date' => string '2014-10-15 14:09:00' (length=19)
'site' => string 'Propinsi Nusa Tenggara Barat' (length=28)
'url_download' => string 'http://ntbprov.sip-ppid.net/index.php/document/download/231' (length=59)
1 =>
array (size=1)
0 =>
array (size=5)
'document_id' => string '7' (length=1)
'document_title' => string 'Data Menara 2014' (length=16)
'document_date' => string '2015-01-08 13:05:00' (length=19)
'site' => string 'Kabupaten Bima' (length=14)
'url_download' => string 'http://bimakab.sip-ppid.net/index.php/document/download/7' (length=57)
how to sort by document_date? I try with usort() but not working.
I am Newbie.

Categories