How to get data in PHP array with foreach? - php

I have data like this and I need to get it to variable for use please suggest to me how to get them to variable
Array
(
[data] => Array
(
[0] => Array
(
[name] => xxxxxxxxxxxx
[category] => yyyyyyyy
[id] => 12345666666
[data] => ABCDE
)
[1] => Array
(
[name] => ZZZZZZZZZZZ
[category] => JJJJJJ
[id] => 88888888888888
[data] => ABCDEHIJK
)
)
)
Thank you vary much.

foreach ($array['data'] as $data) {
echo $data['name'];
echo $data['category'];
...
}
http://php.net/manual/en/control-structures.foreach.php

Related

How to merge three arrays according to common key in php

I have three arrays first array include ids and employees name and second array have monthly collection with employee ids and third array have daily collection with employee id and daily collection I want to merge these array with ids and name and dcollection and monthly collection but the desired output is not coming here my first array $ids is
Array
(
[0] => stdClass Object
(
[id] => 1
[name] => Rohit
)
[1] => stdClass Object
(
[id] => 2
[name] => Emop1
)
[2] => stdClass Object
(
[id] => 3
[name] => Pankaj
)
[3] => stdClass Object
(
[id] => 4
[name] => tejpal singh
)
)
second array $q1 is
Array
(
[0] => stdClass Object
(
[name] => Rohit
[id] => 1
[mcollecton] => 100
)
[1] => stdClass Object
(
[name] => Emop1
[id] => 2
[mcollecton] => 1222
)
)
third array $q2 is
Array
(
[0] => stdClass Object
(
[name] => Rohit
[id] => 1
[dcollecton] => 300
)
[1] => stdClass Object
(
[name] => Emop1
[id] => 2
[dcollecton] => 150
)
)
so far what I have tried
$new_array = array();
foreach($ids as $k) {
$q1n = array("id"=>$k->id,"name"=>$k->name);
foreach($q1 as $k1) {
if($k->id==$k1->id){
$mc = array("mc"=>$k1->mcollecton);
array_merge($q1n,$mc);
}
}
foreach($q2 as $k1){
if($k->id==$k1->id){
$dc = array("dc"=>$k1->dcollecton);
array_merge($q1n,$dc);
}
}
$a = array_merge($q1n,$mc);
$av = array_merge($q1n,$dc);
array_push($new_array,$q1n);
}
but the output is coming as
Array
(
[0] => Array
(
[id] => 1
[name] => Rohit
)
[1] => Array
(
[id] => 2
[name] => Emop1
)
[2] => Array
(
[id] => 3
[name] => Pankaj
)
[3] => Array
(
[id] => 4
[name] => tejpal singh
)
)
I want the output be like
Array
(
[0] => Array
(
[id] => 1
[name] => Rohit
[mcollection] => 100
[dcollection] => 300
)
[1] => Array
(
[id] => 2
[name] => Emop1
[mcollection] => 1222
[dcollection] => 150
)
[2] => Array
(
[id] => 3
[name] => Pankaj
[mcollection] => 0
[dcollection] => 0
)
[3] => Array
(
[id] => 4
[name] => tejpal singh
[mcollection] => 0
[dcollection] => 0
)
)
So I have tried many times but the desired output is not coming . please help me out how to get the desired output.
It seemed like that answer could be modified, or put in a function that you could call multiple times if needed to combine more than two arrays.
There's probably cleaner ways to handle this with array functions like array_merge or array_walk, but this is the general idea of how I might approach it. I haven't tested this, but maybe it's useful.
foreach($first as $key1 => $value){
foreach($second as $key2 => $value2){
// match the ids and check if array key exists on first array
if($value['id'] === $value2['id'] && empty($first[$key2])){
$first[$key][$key2] = $value2;
}
}
}
EDIT: Based on the answer you posted vs the question you asked, are you incrementing the collection numbers or just setting them? In other words why use +=? You should also be able to remove array_merge and array_push.
Below is geared more towards what you're trying to do. I haven't tested this either, but if you run into errors, post your code with the errors returned so that it's easier to debug:
foreach($ids as $k)
{
$thisArray = $newArray[] = array("id"=>$k->id,"name"=>$k->name);
foreach($q1 as $k1)
{
if($k->id == $k1->id && !empty($k1->mcollecton))
{
$thisArray['mc'] = $k1->mcollecton;
}
}
foreach($q2 as $k2)
{
if($k->id == $k2->id && !empty($k2->dcollecton))
{
$thisArray['dc'] = $k2->dcollecton;
}
}
}
// This should have both new collections fields on all array items
print_r($newArray)

How to remove parent array index from array

I want to remove parent array index in array.
Following is my array.
Array
(
[0] => Array
(
[0] => Array
(
[id] => 296
[username] => David0123
[profile_slug] => david-love
)
)
[1] => Array
(
[0] => Array
(
[id] => 297
[username] => Anne_wils
[profile_slug] => anne-chase
)
)
[2] => Array
(
[0] => Array
(
[id] => 300
[username] => malina001
[profile_slug] => malina-reid
)
)
)
And I want like this way..
Array(
[0] => Array
(
[id] => 296
[username] => David0123
[profile_slug] => david-love
)
[1] => Array
(
[id] => 297
[username] => Anne_wils
[profile_slug] => anne-chase
)
[2] => Array
(
[id] => 300
[username] => malina001
[profile_slug] => malina-reid
)
)
I used following script for it but not work.
$myMainArray = json_decode(json_encode($allEscorts),true);
$i=0;
foreach( array_values($myMainArray) as $k=> $val){
echo $val[$i]['id'];
$i++;
}
I want to display data each element but first i have to remove parent array indexes.
You can use array_map to pull values up one level
$myMainArray = json_decode(json_encode($allEscorts),true);
$myMainArray = array_map(function($el) {
return $el[0];
}, $myMainArray);
You should check if the first array could be generate as you wish.
If not you can use array_map to get the first index from the inner-array.
for example:
$result = array_map(function($item){
return $item[0]; // always return the first array-index
}, $first_array);

How to Fetch data from Array in WordPress

Array
(
[0] => stdClass Object
(
[meta_id] => 23233
[post_id] => 4467
[meta_key] => first_name
[meta_value] => Daud
)
)
How can I echo post_id from this array for all posts using while or foreach statement?
Array
(
[classic-editor-remember] => Array
(
[0] => classic-editor
)
[_edit_lock] => Array
(
[0] => 1582905950:5
)
[_edit_last] => Array
(
[0] => 5
)
[_thumbnail_id] => Array
(
[0] => 4376
)
[slide_template] => Array
(
[0] => default
)
[_yoast_wpseo_content_score] => Array
(
[0] => 30
)
[_yoast_wpseo_primary_advisor_category] => Array
(
[0] =>
)
[title] => Array
(
[0] => Demo Daniel Wrenne, CFP, ChFC
)
[designation] => Array
(
[0] => Wrenne Financial Planing, LLC Lexington, KY
)
[client_specialities] => Array
(
[0] => Gen Y/Millennials, Medical Professionals
)
[address] => Array
(
[0] => 3223 S LEHI DR
)
[phone_number] => Array
(
[0] => 64646446486
)
[email_address] => Array
(
[0] => demo#demo.com
)
[website_url] => Array
(
[0] => a:3:{s:3:"url";s:23:"https://www.google.com/";s:4:"text";s:20:"View Advisor Profile";s:6:"target";s:4:"none";}
)
[first_name] => Array
(
[0] => Daud
)
[last_name] => Array
(
[0] => Yahya
)
)
And how can I get las_name, first_name, email, address, website url, specialities, designation and title from the above array using and loop like while or foreach loop.
This is less a WordPress question and a basic PHP foreach question.
The first example you have is an Object, so you need to access the properties, e.g. meta_id, post_id like:
// THIS IS JUST AN EXAMPLE. YOUR VARIABLE WILL CHANGE BASED ON HOW YOU GOT THE DATA. `$object_array` is how you got the data to begin with.
foreach( $object_array as $object ) {
$post_id = $object->post_id;
echo $post_id;
}
For your second example, since there is only one array key inside each array key, you would set it up like this:
// Example. you would use whatever you used to get the array to begin with as the `$array`.
foreach ($array as $item ) {
$last_name = $item['last_name'][0];
$first_name = $item['first_name'][0];
....
}

How to check array object and array?

First array:
[VariationSpecificsSet] => SimpleXMLElement Object
(
[NameValueList] => Array
(
[0] => SimpleXMLElement Object
(
[Name] => Size
[Value] => Array
(
[0] => 5FT King Size
[1] => 4FT6 Double
)
)
[1] => SimpleXMLElement Object
(
[Name] => Main Colour
[Value] => Array
(
[0] => Brown
[1] => Black
)
)
)
)
Second Array:
[Variation] => SimpleXMLElement Object
(
[StartPrice] => 14.99
[Quantity] => 12
[VariationSpecifics] => SimpleXMLElement Object
(
[NameValueList] => SimpleXMLElement Object
(
[Name] => Size
[Value] => No.10-1M
)
)
)
examine above two arrays
i want to store value NameValueList in database but the problem is sometimes it is SimpleXMLElement Object and sometimes it is Array
how can i store them ...??
You can detect is by is_array().
$myVal=$test['NameValueList'];
if(is_array($myVal) && count($myVal)>0){
foreach($myVal as $item){
echo $item->Name.":".echo $item->Value;
}
} else {
echo $myVal->Name.":".echo $myVal->Value;
}
Did you tried using json_encode like below.
You can convert the object to array.
$array=json_decode(json_encode($object),true);

PHP echo out multidimensional array from facebook var

I have a facebook array and I am trying to echo out the names and ID.
All I have is this array: $friends_list.
When I use this code:
print_r($friends_list);
Then the following comes out below: But how do I loop thru these? Or what if I just wanted to find out how many names there are or just call on one var by id. In short how do I echo out $friends_list which is a multiD array?
Array ( [data] => Array ( [0] => Array ( [name] => Wendy Cukierski [id] => 524320383 ) [1] => Array ( [name] => Leticia Mercado Correa [id] => 537763225 ) [2] => Array ( [name] => Olivia Urbina [id] => 538711855 ) [3] => Array ( [name] => Ruth Albrecht [id] => 541610111 ) [4] => Array ( [name] => Josh Brahm [id] => 577546485 ) [5] => Array ( [name] => Kim Yu [id] => 583515871 ) [6] => Array ( [name] => SisterTracey Dugas [id] => 643567171 ) [97] => Array ( [name] => Mary Martinez [id] => 100004696266210 ) ) [paging] => Array ( [next] => https://graph.facebook.com/1566027944/friends?limit=5000&offset=5000&__after_id=100004696266210 ) )
DUPLICATE --> How to store a Facebook user's friends list in MySQL using PHP?
$friends_list = file_get_contents('https://graph.facebook.com/me/friends?access_token=' . $atoken );
$friends_list_array = json_decode($friends_list,true);
$arr= $friends_list_array['data'];
$friend_ids_arr = array();
foreach($arr as $friend) {
$friend_ids_arr[] = $friend['id'];
}
$friend_ids = implode("," , $friend_ids_arr);
echo $friend_ids; // output: id1,id2,id3...etc

Categories