I have a multidimensional array as shown below
Array
(
[0] => Array
(
[0] => stdClass Object
(
[id] => 237
)
[1] => stdClass Object
(
[id] => 228
)
)
[1] => Array
(
[0] => stdClass Object
(
[id] => 247
)
[1] => stdClass Object
(
[id] => 238
)
)
)
I want to convert into single array as shown below
Array
(
[0] => stdClass Object
(
[id] => 237
)
[1] => stdClass Object
(
[id] => 228
)
[2] => stdClass Object
(
[id] => 247
)
[3] => stdClass Object
(
[id] => 238
)
)
I have tried with the following solution Convert multidimensional array into single array
But the result am not getting Its Coming Null
How to get the desired result for the above input.
Any help appreciated.
Try foreach loop then array_merge()
$result = [];
foreach ($array as $value) {
$result = array_merge($result, $value);
}
var_dump($result);
Hope this one works
function array_flattern($arr) {
$returnArr=[];
foreach($arr as $k=>$v) {
$returnArr = array_merge($returnArr, $v);
}
return $returnArr;
}
Related
I've got an array of arrays they have a different value.
i want to print [name] field values of array ...
every time i use foreach my result not good...!
Array
(
[und] => Array
(
[0] => Array
(
[tid] => 5683
[taxonomy_term] => stdClass Object
(
[tid] => 5683
[name] => deded
)
)
[1] => Array
(
[tid] => 15143
[taxonomy_term] => stdClass Object
(
[tid] => 15143
[name] => dedeededswswsw
)
)
)
)
my code :
$array= ($array['und']);
foreach($array as $newarray){
print_r ($newarray);
}
thanks for your helps
You will notice that the output has more levels before you get to the name field. If you follow the structure
Array
(
[tid] => 5683
[taxonomy_term] => stdClass Object
(
[tid] => 5683
[name] => deded
)
)
you would need to output...
foreach($array as $newarray){
echo $newarray['taxonomy_term']->name.PHP_EOL;
}
I'm looking for the way to replace my array.
My first array below:
$arr1 = Array
(
[0] => stdClass Object
(
[values] => Array
(
[0] => stdClass Object
(
[field_value] => Green
[count] => 0
)
[1] => stdClass Object
(
[field_value] => Red
[count] => 0
)
)
)
[1] => stdClass Object
(
[values] => Array
(
[0] => stdClass Object
(
[field_value] => Plastic
[count] => 0
)
[1] => stdClass Object
(
[field_value] => Metall
[count] => 0
)
)
)
My second array:
$arr2 = Array
(
[0] => 2
[1] => 6
[2] => 3
[3] => 4
)
And I would like to get this:
Array
(
[0] => stdClass Object
(
[values] => Array
(
[0] => stdClass Object
(
[field_value] => Green
[count] => 2
)
[1] => stdClass Object
(
[field_value] => Red
[count] => 6
)
)
)
[1] => stdClass Object
(
[values] => Array
(
[0] => stdClass Object
(
[field_value] => Plastic
[count] => 3
)
[1] => stdClass Object
(
[field_value] => Metall
[count] => 4
)
)
)
I have tried to use array_map function but without any success.
array_map(function($a,$b){$a = $b; return $a;}, $arr1, $arr2);
Thanks!
$array=$arr[0]->values;
$new_array=array();
foreach($array as $key=>$val)
{
$new_array[$key]=$val;
$new_array[$key]->count=$arr2[$key];
}
$result=array();
$result[0]->values=$new_array;
Create your own function as needed, and customize as you like, see below example:
function buildMyArray($array1, $array2)
{
foreach($array1[0]->values as $key => $value){
$array1[0]->values[$key]['count'] = $array2[$key];
}
return $array1;
}
and you may call it like:
$result = buildMyArray($arr1, $arr2);
If you want to use array_map, then something should be like below:
$arr1[0]->values = array_map(function($v, $k) use ($arr2) {
// if not found in $arr2, remain the original value.
$v->count = isset($arr2[$k]) ? $arr2[$k] : $v->count;
return $v;
}, $arr1[0]->values, array_keys($arr1[0]->values));
How would i flatten the following array. I want to get rid of 0 and 1, and just have 0-5 as they keys. I am using php by the way.
Array
(
[0] => Array
(
[0] => Array
(
[id] => 182
)
[1] => Array
(
[id] => 183
)
[2] => Array
(
[id] => 185
)
[3] => Array
(
[id] => 184
)
[4] => Array
(
[id] => 186
)
)
[1] => Array
(
[0] => Array
(
[id] => 171
)
)
)
$flatArray = array();
foreach ($array as $l1Array){
foreach ($l1Array as $k => $v){
$flatArray[]=$v;
}
}
How on Earth do I get the ids, and names from this array (possibly by using a ForEach construct)?
stdClass Object (
[data] => Array (
[0] => stdClass Object ( [name] => Corinne Culbard [id] => 5005561918 )
[1] => stdClass Object ( [name] => Tom West [id] => 5034297044 )
[2] => stdClass Object ( [name] => Sabri Tasci [id] => 5057373639 )
Is this not working?
foreach ($object->data as $item) {
$name = $item->name;
$id = $item->id;
}
Array (
[0] => stdClass Object (
[name] => query1
[fql_result_set] => Array ( [0] => stdClass Object ( [fan_count] => 226 ) ) )
[1] => stdClass Object (
[name] => query2
[fql_result_set] => Array ( [0] => stdClass Object ( [fan_count] => 247 ) ) )
[2] => stdClass Object (
[name] => query3
[fql_result_set] => Array ( [0] => stdClass Object ( [fan_count] => 580 ) ) )
[3] => stdClass Object (
[name] => query4
[fql_result_set] => Array ( [0] => stdClass Object ( [fan_count] => 10 ) ) )
[4] => stdClass Object (
[name] => query5
[fql_result_set] => Array ( [0] => stdClass Object ( [fan_count] => 508 ) ) )
)
How I can parse this result ?
I searched, but I didn't found out how I could do it.
Assuming your array variable is called $data:
foreach ($data as $object) {
$name = $object->name;
$result_set = $object->fql_result_set;
$fan_count = $result_set[0]->fan_count;
// Do what you need to with the data here
}
Hope that helps