I need help traversing an array within array, I only need to loop through certain arrays, for example, how would I just lop through the names array?
Array
(
[#total_records] => 10
[#total_matching_records] => 10
[#available_records] => 200
[#available_matching_records] => 12
[query] => Array
(
[summary] => Array
(
[emails] => Array
(
[0] => Array
(
[content] => jonathan.lyon#gmail.com
)
)
)
)
[results] => Array
(
[person] => Array
(
[#match_score] => 1
[summary] => Array
(
[names] => Array
(
[0] => Array
(
[first] => Jonathan
[last] => Lyon
[display] => Jonathan Lyon
)
[1] => Array
(
[first] => Jonathan
[last] => Jordan
[display] => Jonathan Jordan
)
)
I have tried this but can't get it to work:-
foreach($json_output['results']['person']['summary']['names'] as $key => $val) {
echo $key.": ".$val."</br>";
}
Any help would be greatly appreciated.
Thanks
Jonathan
In your example you trying to echo $key. Key in your case $key is array index (integer). Are you sure you realy need that?
You nedd to change your code to:
foreach($json_output['results']['person']['summary']['names'] as $val) {
echo $val['display']."</br>";
}
Have you tried this
foreach($json_output['results']['person']['summary']['names'] as $key => $val) {
echo $key.": ".$val['display']."</br>";
}
?
Are you getting any error output? That would help a lot. I can see also that $val in this case is an array so you don't want to echo that.
Related
I am trying to decode json and then break the value with foreach loop.
Here is my script.
<?php $new_data=json_decode($pay_data->l1_level,true);
print_r($new_data);
?>
this print return array.
Array ( [name] => Array ( [0] => sanjay [1] => susanchen [2] => mabelzhen) [product-price] => Array ( [0] => 250 [1] => 250 [2] => 250) )
I am trying to break this foreach loop and get name and price value . I am trying to this away
foreach($new_data as $value){
echo $value->name;
echo $value->product_price ;
}
But This is not working.Any Help will be Appreciate.
When you use foreach($new_data as $value), the $value doesn't have the key.
Please try the following code.
foreach($new_data as $value){
print_r($value);
}
the result is
Array ( [0] => sanjay [1] => susanchen [2] => mabelzhen ) Array ( [0] => 250 [1] => 250 [2] => 250 )
i am trying to display array values return from facebook api. array is stored in $result below is my array
Array
(
[data] => Array
(
[0] => Array
(
[name] => Ravi Shankar
[id] => 10206576743272319
)
)
[paging] => Array
(
[next] => https://graph.facebook.com/v2.5/1705944052976370/friends?limit=25&offset=25&__after_id=enc_AdBV9ZBlJwwdjBL8iWeIAZBSxDqJO0gvQWS45qwBBg1X8tCbZAoj9Cz506ZCGuDnddOL07MZD
)
[summary] => Array
(
[total_count] => 4628
)
)
i tried using foreach but confused
foreach($result as $key=>$value){
echo $key."-".$value."<br />";
}
i want to display the result like below:
name id
xxxx 123456
yyyy 173453
Write your foreach loop as below:-
foreach($result['data'] as $key=>$value){
echo $value['name'].'-'.$value['id']."<br>";
}
Hope it will help you :)
You need to use foreach in the $result["data"]:
foreach ($result["data"] as $friend) {
echo "{$friend["id"]} => {$friend["name"]}";
}
The each $friend variable is an array, of this format:
Array
(
[name] => Ravi Shankar
[id] => 10206576743272319
)
Hello Stack Overflow community,
I am trying to update values from array1 with a newly ordered array2 and output the final array as array2. I have been tying to figure this out for days now but cannot seem to get it. I am new to php as you can most likely tell. Can anyone help me?
Here are the two arrays- 'array1' is first and 'array2' follows:
Array (
[0] => Array
(
[id] => bbb
[hammer] => $1,000
)
[1] => Array
(
[id] => ccc
[hammer] => $678
)
[2] => Array
(
[id] => aaa
[hammer] => $222
)
) Array (
[0] => Array
(
[id] => aaa
[hammer] => GBP 135
)
[1] => Array
(
[id] => bbb
[hammer] => GBP 610
)
[2] => Array
(
[id] => ccc
[hammer] => GBP 413
)
)
Now my code tries to update the newly reordered 'id' values in the second array with the 'hammer' values from the first array. Here is my code:
foreach($array2 as $key => $val) {
$a = $array2[$key]['id'];
$hammer_a = $array2[$key]['hammer'];
foreach($array1 as $key => $val) {
$b = $array1[$key]['id'];
if($a===$b){
$hammer_b = $array1[$key]['hammer'];
$array2[$key]['hammer'] = $hammer_b;
}
}
}
However as this code stands I am left with an undesired result below. I would like 'id' 'aaa' to have its original 'hammer' value of $222 and so on for the other 'id's:
Array
(
[0] => Array
(
[id] => aaa
[hammer] => $1,000
)
[1] => Array
(
[id] => bbb
[hammer] => $678
)
[2] => Array
(
[id] => ccc
[hammer] => $222
)
)
Can anyone please tell me what I am doing wrong?
Change the name of $key and $val in the second loop. They are overriding each other.
Don't use $key in your inner foreach loop. You want the key for array2.
There is also this option, with a lot less code:
$items[0] = array('id'=>'bbb','hammer'=>'$1,000');
$items[1] = array('id'=>'ccc','hammer'=>'$678');
$items[2] = array('id'=>'aaa','hammer'=>'$222');
function cmp($a, $b) {
return strcmp($a['id'], $b['id']);
}
echo '<pre>';
var_dump($items);
usort($items, "cmp");
var_dump($items);
echo '</pre>';
Im a bit lost or mind is not working as it should. I read other questions around but can get mine to work.
I got this array:
Array
(
[89] => Array
(
[0] => 16
[1] => 2
)
)
And i got this :
Array
(
[84] => Array
(
[0] => 2
)
[83] => Array
(
[0] => 2
)
[87] => Array
(
[0] => 2
[1] => 3
)
[88] => Array
(
[0] => 2
)
[89] => Array
(
[0] => 2
)
[90] => Array
(
[0] => 2
)
)
I should get all results but on key 89 i should get the value from first array.
Array
(
[84] => Array
(
[0] => 2
)
[83] => Array
(
[0] => 2
)
[87] => Array
(
[0] => 2
[1] => 3
)
[88] => Array
(
[0] => 2
)
[89] => Array
(
[0] => 16
[1] => 2
)
[90] => Array
(
[0] => 2
)
)
Array merge wont work :( .
Also after i get the result if the first array its :
Array
(
[89] => Array
(
[1] => 2
)
)
The resulting array should update to one record.
Im sure its a 1 min code for you gurus but arrays always been a pain for me.
Thanks
UPDATE : if i use array_merge_recursive it wont keep my keys :
print_r(array_merge_recursive($array1,$array2));
Array
(
[0] => Array
(
[0] => 16
[1] => 2
)
[1] => Array
(
[0] => 2
)
[2] => Array
(
[0] => 2
)
[3] => Array
(
[0] => 2
[1] => 3
)
[4] => Array
(
[0] => 2
)
[5] => Array
(
[0] => 2
)
[6] => Array
(
[0] => 2
)
)
array_merge_recursive should do the job; look at the documentation for more pointers: http://www.php.net/manual/en/function.array-merge-recursive.php
EDIT
The function behaved differently than I initially thought, hereĀ“s a different version of the function to solve your problem:
function array_merge_recursive_distinct(array &$array1, array &$array2) {
$merged = $array1;
foreach($array2 as $key => &$value) {
if(is_array($value) && isset($merged[$key]) && is_array($merged[$key])) {
$merged[$key] = array_merge_recursive_distinct($merged[$key], $value);
} else {
$merged[$key] = $value;
}
}
return $merged;
}
Thanks to the community of php.net http://www.php.net/manual/en/function.array-merge-recursive.php#92195
If I understand it correctly, adding arrays should suffice:
$result_array = $array1 + $array2;
In contrast to array_merge, it won't overwrite values in first array and won't renumber numerical keys.
$newarray=array();
foreach(array_merge($array1,$array2) as $k=>$arr){
$newarray[$k]=array_merge($array1[$k],$array2[$k]);
}
foreach($firstArr as $key=>$val){
if(in_array($key,$secondArray)){
$secondArray[$key] = $firstArr[$key];
}
}
Try this may help you.
I have a ldap request that returns an array, however the returned array is full of "counts" and odd pointers, as this application is an API designed to be used with Javascript on mobile devices, the less text there is the better.
What is the most efficient way to strip out all the values that don't have the key count? As well as the ones that just seem to be saying what the keys are (eg [0] => cn)?
Array
(
[status] => OK
[count] => 4
[results] => Array
(
[count] => 3
[0] => Array
(
[cn] => Array
(
[count] => 1
[0] => James Bee
)
[0] => cn
[umanprimaryou] => Array
(
[count] => 1
[0] => Awesome School
)
[1] => umanprimaryou
[ou] => Array
(
[count] => 2
[0] => School of Awesome
[1] => Faculty of Engineering
)
etc...
Aiming for
Array
(
[status] => OK
[results] => Array
(
[0] => Array
(
[cn] => Array
(
[0] => James Bee
)
[umanprimaryou] => Array
(
[0] => Awesome School
)
[ou] => Array
(
[0] => School of Awesome
[1] => Faculty of Engineering
)
etc...
For further explanation I am wanting to unset all the [count] => value pairs and if possible the values such as [0] => cn in the results array.
Untested code:
function strip_count(array $arr) {
foreach ($arr as $key => $value) {
if (is_array($arr[$key])) {
strip_count($arr[$key]);
} else {
if ($key == 'count') {
unset($arr[$key]);
}
}
}
}
Edit: I wrote this before you edited your question, but it shouldn't be too hard to modify this recursive function to strip out the other things you wish to remove as well.
Might wanna look into using array_walk_recursive and check if $key == 'count' in your user defined function.