Get Specific Value from Multidimensional Associative JSON - php

I am trying to get a specific value from a JSON response and am having trouble figuring out how to target it. I can loop through it but I want to only output a single value based on the key as the response is dynamic and not always guaranteed to have the same output.
I have converted the JSON response to an array and here is an example of what I have at this point:
Array (
[0] => Array (
[rel] => advertisements
[href] => https://api.teamsnap.com/v3/advertisements
)
[1] => Array (
[rel] => active_season_team
[href] => https://api.teamsnap.com/v3/teams
)
[2] => Array (
[rel] => assignments
[href] => https://api.teamsnap.com/v3/assignments
)
[3] => Array (
[rel] => availabilities
[href] => https://api.teamsnap.com/v3/availabilities
)
)
I need to be able to echo the "href" value by the key "rel". I have tried:
foreach($links as $key => $value) {
echo $key['advertisements'];
}
But need a way to say $links as $key->rel = $value->http (thats the logic I can come up with)

You can use array_search on the rel column (extracted using array_column) to find the key of a matching value (e.g. advertisements). If it exists, you can then access the value directly:
if(($key = array_search('advertisements', array_column($links, 'rel'))) !== false) {
echo $links[$key]['href'];
}
Output
https://api.teamsnap.com/v3/advertisements
Demo on 3v4l.org
For more flexibility, you could write this as a function:
function get_link($links, $cat) {
if(($key = array_search($cat, array_column($links, 'rel'))) !== false) {
return $links[$key]['href'];
}
else {
return '';
}
}
echo get_link($links, 'active_season_team');
Output:
https://api.teamsnap.com/v3/teams
Demo on 3v4l.org

I didn't understand your requirements but as per syntax, In your case key will be index of array you can get data as below:
foreach($links as $link){
echo $link['rel'].' = '.$link['href'];
}

Related

get the value from the associtive array in table

Array
(
[0] => Array
(
[datas] => Array
(
[res] => 1
[rows] => Array
(
[0] => stdClass Object
(
[a] => 11
[b] => 901
[c] => 2
[d] => 2
[e] => A
[f] => BT
[g] => arushi
[h] => arushi#gmail.com
[i] => 123445
[j] => 12355.jpg
)
)
)
)
[1] => Array
(
[datas] => Array
(
[res] => 1
[rows] => stdClass Object
(
[person_name] => arushi
)
)
)
)
if i get the value in that kind off array how can i get the value of
both partially with different variable m not able to understand the
structure of the array..
need to get the value in table seperatly in different different table with same page how i can get the values
You would need to do a foreach loop. The only problem is that if you want to make it dynamic and grab the data by itself without you telling it what to get. If not it means you would have to know exactly what you need and the structure of your results. But seeing that there is a pattern you can do some checks until you reach rows. For example ($array would be the variable that has the data you provided):
foreach ($array AS $arr) {
// To make you function/check work faster,
// before even entering to fetch for data
// you can check if it has any data, else you
// probably will end up with an error
if (isset ($arr ['datas']) && $arr ['datas']) {
foreach ($arr ['datas'] AS $data) {
if (isset ($arr ['res']) && 0 < $arr ['res']) {
// here is where it gets tricky because
// from you example its either an array or an object
// but the kool part about it is that you can convert an object into an array
// or just leave it as is because the foreach will take care of it either way :)
// first check it it is an object and convert if yes
$row = $arr ['rows'];
if (!is_array ($row))
$row = (array)$row; // converting it to array is super easy :)
foreach ($row AS $key=>$dat) {
// from here your result can either be another array or just data
// so you run the check once more
if (is_array ($dat)) {
foreach ($dat AS $k=>$d) {
echo "data for $k is $d";
}
}
else
echo "data for $key is $dat";
}
}
}
}
}
You can use foreach to loop through the arrays which have keys starting from 0 to n. So that you can foreach through the main array to get the datas array for all the keys. For getting rows childs you can use another foreach inside it to get each items. But for rows it is object , so you have to use -> to select the key. see below example code. But on your array the second array consist different formate in rows array. so make it like a common formate to loop through it easily.
For eg :-
foeach($parent_array as $array){
$data=$array['datas'];
$res = $data['res'];
$rows=$data['rows'];
foreach($rows as $row){
echo $row->a; // to get the value of key a = 11
}
}

Create multidimensional array from standard array

Most likely I'm doing this wayyyyyy too complicated. But I'm in the need of converting multiple arrays to multidimensional array key's. So arrays like this:
Array //$original
(
[0] => 500034
[1] => 500035 //these values need to become
//consecutive keys, in order of array
)
Needs to become:
Array
(
[50034][50035] => array()
)
This needs to be done recursively, as it might also require that it becomes deeper:
Array
(
[50034][50036][50126] => array() //notice that the numbers
//aren't necessarily consecutive, though they are
//in the order of the original array
)
My current code:
$new_array = array();
foreach($original as $k => $v){ //$original from first code
if((gettype($v) === 'string' || gettype($v) === 'integer')
&& !array_key_exists($v, $original)){ //check so as to not have illigal offset types
$new_array =& $original[array_search($v, $original)];
echo 'In loop: <br />';
var_dump($new_array);
echo '<br />';
}
}
echo "After loop <br />";
var_dump($new_array);
echo "</pre><br />";
Gives me:
In loop:
int(500032)
In loop:
int(500033)
After loop
int(500033)
Using this code $new_array =& $original[array_search($v, $original)]; I expected After loop: $new_array[50034][50035] => array().
What am I doing wrong? Been at this for hours on end now :(
EDIT to answer "why" I'm trying to do this
I'm reconstructing facebook data out of a database. Below is my own personal data that isn't reconstructing properly, which is why I need the above question answered.
[500226] => Array
(
[own_id] =>
[entity] => Work
[name] => Office Products Depot
[500227] => Array
(
[own_id] => 500226
[entity] => Employer
[id] => 635872699779885
)
[id] => 646422765379085
)
[500227] => Array
(
[500228] => Array
(
[own_id] => 500227
[entity] => Position
[id] => 140103209354647
)
[name] => Junior Programmer
)
As you can see, the ID [500227] is a child of [500226], however, because I haven't got the path to the child array, a new array is created. The current parentage only works to the first level.
[own_id] is a key where the value indicates which other key should be its parent. Which is why the first array ([500226]) doesn't have a value for [own_id].
If you want to do something recursively, do it recursively. I hope that's what you meant to do.
public function pivotArray($array, $newArray)
{
$shifted = array_shift($array);
if( $shifted !== null )
{
return $this->pivotArray($array, array($shifted=>$newArray));
}
return $newArray;
}
$array = array(432, 432532, 564364);
$newArray = $this->pivotArray($array, array());
Edit: After the question's edit it doesn't seem to be very relevant. Well, maybe someone will find it useful anyway.

PHP in_array() unexpected result

This is the $feed_content array
Array
(
[0] => Array
(
[id] => 1
[link] => http://www.dust-off.com/10-tips-on-how-to-love-your-portable-screens-keeping-them-healthy
)
[1] => Array
(
[id] => 2
[link] => http://www.dust-off.com/are-you-the-resident-germaphobe-in-your-office
)
)
----Another array----------
This is the $arrFeeds array
Array
(
[0] => Array
(
[title] => 10 Tips on How to Love Your Portable Screens (Keeping them Healthy)
[link] => http://www.dust-off.com/10-tips-on-how-to-love-your-portable-screens-keeping-them-healthy
)
[1] => Array
(
[title] => Are You the Resident Germaphobe in Your Office?
[link] => http://www.dust-off.com/are-you-the-resident-germaphobe-in-your-office
)
)
Here's my code:
foreach( $arrFeeds as $key2 => $value2 )
{
$feed_content = $feed->get_feed_content( $value['id'] );
if( !in_array( $value2['link'], $feed_content ) )
{
echo "not match!";
}
}
Question:
Why is it that the code always enters into the if statement even if $feed_content link value has the value of the $arrFeeds link?
My expected result should be that my code would tell me if the $feed_content link value is not in the $arrFeeds.
By the way, the $feed_content code returns an array that I specified above.
What should be the problem with this one. Thanks in advance! :)
that's because the elements of your array called $feed_content are associative arrays also (with the id and link keys)
you are checking if the link (a string) is equal to any of the elements in the array (all of them arrays)
Edit:
To achieve what you want you can use a "hack". Instead of in_array you can use something like:
$search_key = array_search(array('id'=>true,'link'=>$value2['link']), $feed_content);//use true for the id, as the comparison won't be strict
if (false !== $search_key)//stict comparison to be sure that key 0 is taken into account
{
echo 'match here';
}
this thing relies on the fact that you can use an array for the search needle for the array_search function and that the comparison won't be strict, so true will match any number (except 0, but I guess that you don't use 0 as an id)
this way the only field that will really matter is the one for the link
after that you need to use a strict comparison this time to be sure that if the found key is 0 you will use it
in_array doesn't search recursively. It sees $feed_content as
Array
(
[0] => Array
[1] => Array
)
Now you could extend your if clause to foreach through $feed_content:
$found = false;
foreach($feed_content as $feedArr)
{
if(in_array($value2['link'], $feedArr))
{
$found = true;
}
}
if(!$found) echo "not match!";
if( !in_array( $value2['link'], $feed_content ) )
if( !in_array( $value2['link'], array_values($feed_content)) )

PHP - Create a single array from a multidimensional array based on key name

I know there are a lot of answers on multi-dimensional arrays but I couldn't find what I was looking for exactly. I'm new to PHP and can't quite get my head around some of the other examples to modify them. If someone could show me the way, it would be much appreciated.
An external service is passing me the following multidimensional array.
$mArray = Array (
[success] => 1
[errors] => 0
[data] => Array (
[0] => Array (
[email] => me#example.com
[id] => 123456
[email_type] => html
[ip_opt] => 10.10.1.1
[ip_signup] =>
[member_rating] => X
[info_changed] => 2011-08-17 08:56:51
[web_id] => 123456789
[language] =>
[merges] => Array (
[EMAIL] => me#example.com
[NAME] => Firstname
[LNAME] => Lastname
[ACCOUNT] => ACME Ltd
[ACCMANID] => 123456adc
[ACCMANTEL] => 1234 123456
[ACCMANMAIL] => an.other#example.com
[ACCMANFN] => Humpty
[ACCMANLN] => Dumpty
)
[status] => unknown
[timestamp] => 2011-08-17 08:56:51
[lists] => Array ( )
[geo] => Array ( )
[clients] => Array ( )
[static_segments] => Array ( )
)
)
)
The only information I'm interested in are the key/value pairs that are held in the array under the key name 'merges'. It's about the third array deep. The key name of the array will always be called merges but there's no guarantee that its location in the array won't be moved. The number of key/value pairs in the merges array is also changeable.
I think what I need is a function for array_walk_recursive($mArray, "myfunction", $search);, where $search holds the string for the Key name (merges) I'm looking for. It needs to walk the array until it finds the key, check that it holds an array and then (preserving the keys), return each key/value pair into a single array.
So, for clarity, the output of the function would return:
$sArray = Array (
[EMAIL] => me#example.com
[NAME] => Firstname
[LNAME] => Lastname
[ACCOUNT] => ACME Ltd
[ACCMANID] => 123456adc
[ACCMANTEL] => 1234 123456
[ACCMANMAIL] => an.other#example.com
[ACCMANFN] => Humpty
[ACCMANLN] => Dumpty
)
I can then move on to the next step in my project, which is to compare the keys in the single merges array to element IDs obtained from an HTML DOM Parser and replace the attribute values with those contained in the single array.
I probably need a foreach loop. I know I can use is_array to verify if $search is an array. It's joining it all together that I'm struggling with.
Thanks for your help.
Would this work?
function find_merges($arr)
{
foreach($arr as $key => $value){
if($key == "merges") return $value;
if(is_array($value)){
$ret = find_merges($value);
if($ret) return $ret;
}
}
return false;
}
It would do a depth-first search until you either ran out of keys or found one with the value merges. It won't check to see if merges is an array though. Try that and let me know if that works.
Here is a general purpose function that will work it's way through a nested array and return the value associated with the first occurance of the supplied key. It allows for integer or string keys. If no matching key is found it returns false.
// return the value a key in the supplied array
function get_keyval($arr,$mykey)
{
foreach($arr as $key => $value){
if((gettype($key) == gettype($mykey)) && ($key == $mykey)) {
return $value;
}
if(is_array($value)){
return get_keyval($value,$mykey);
}
}
return false;
}
// test it out
$myArray = get_keyval($suppliedArray, "merges");
foreach($myArray as $key => $value){
echo "$key = $value\n";
}
A recursive function can do this. Returns the array or FALSE on failure.
function search_sub_array ($array, $search = 'merges') {
if (!is_array($array)) return FALSE; // We're not interested in non-arrays
foreach ($array as $key => $val) { // loop through array elements
if (is_array($val)) { // We're still not interested in non-arrays
if ($key == $search) {
return $val; // We found it, return it
} else if (($result = search_sub_array($array)) !== FALSE) { // We found a sub-array, search that as well
return $result; // We found it, return it
}
}
}
return FALSE; // We didn't find it
}
// Example usage
if (($result = search_sub_array($myArray,'merges')) !== FALSE) {
echo "I found it! ".print_r($result,TRUE);
} else {
echo "I didn't find it :-(";
}
So you want to access an array within an array within an array?
$mergeArray = NULL;
foreach($mArray['data'] as $mmArray)
$mergeArray[] = $mmArray['merges'];
Something like that? If merges is always three deep down, I don't see why you need recursion. Otherwise see the other answers.
Here's another approach, mostly because I haven't used up my iterator quota yet today.
$search = new RegexIterator(
new RecursiveIteratorIterator(
new ParentIterator(new RecursiveArrayIterator($array)),
RecursiveIteratorIterator::SELF_FIRST),
'/^merges$/D', RegexIterator::MATCH, RegexIterator::USE_KEY
);
$search->rewind();
$merges = $search->current();
array_walk_recursive() is brilliant for this task! It doesn't care what level the key-value pairs are on and it only iterates the "leaf nodes" so there is not need to check if an element contains a string. Inside of the function, I am merely making a comparison on keys versus the array of needles to generate a one-dimensional result array ($sArray).
To be clear, I am making an assumption that you have predictable keys in your merges subarray.
Code: (Demo)
$needles=['EMAIL','NAME','LNAME','ACCOUNT','ACCMANID','ACCMANTEL','ACCMANMAIL','ACCMANFN','ACCMANLN'];
array_walk_recursive($mArray,function($v,$k)use(&$sArray,$needles){if(in_array($k,$needles))$sArray[$k]=$v;});
var_export($sArray);
Output:
array (
'EMAIL' => 'me#example.com',
'NAME' => 'Firstname',
'LNAME' => 'Lastname',
'ACCOUNT' => 'ACME Ltd',
'ACCMANID' => '123456adc',
'ACCMANTEL' => '1234 123456',
'ACCMANMAIL' => 'an.other#example.com',
'ACCMANFN' => 'Humpty',
'ACCMANLN' => 'Dumpty',
)

how to output the array in php?

Array
(
[menu-162] => Array
(
[attributes] => Array
(
[title] => example1
)
[href] => node/13
[title] => test1
)
[menu-219] => Array
(
[attributes] => Array
(
[title] => example2
)
[href] => node/30
[title] => test2
)
)
If I assign the above array to a variable named $hello, now, I want to use a loop only output the menu-162, menu-219.
If I want to only output the attributes title value, if I only want to output the href's value.
How do I write these loops?
foreach ($hello as $item) {
$attr = $item['attributes']['title'];
$href = $item['href'];
echo "attr is {$attr}";
echo "href is {$href}";
}
That should output the attr and href.
You can access titles value within the attributes array like so: $hello['menu-162']['attributes']['title'] and for any other 'menu' you can substitute menu-162 with the appropriate menu-number combination. As for the href a simple $hello['menu-162']['href']
As for a loop to access both the values a simple foreach should suffice:
foreach($hello as $value) {
echo $value['attributes']['title'];
echo $value['href'];
}
foreach($hello as $key => $value) {
switch($key) {
case 'menu-162':
case 'menu-219':
if($value['href'] && $value['attribute'] && $value['attribute']['title']) {
$href = $value['href'];
$attr = $value['attribute']['title'];
}
break;
default:
continue; //didn't find it
break;
}
}
If you do not need the specific menu finding, remove the switch statement. If you do need the specific ids using this is the more scalable solution, and is faster than a nested if. It will also not create notices for variables that don't exist and will only return if both attribute title, and href exist.

Categories