How do I write PHP code to manually create this array?
Array (
[0] => Array
(
[Id] => 2
[Fruit] => Apple
)
[1] => Array
(
[Id] => 5
[Fruit] => Orange
)
)
$arr = array();
$arr[] = array("Id" => 2, "Fruit" => "Apple");
$arr[] = array("Id" => 5, "Fruit" => "Orange");
$array = array();
$array[] = array(
'id'=>3,
'Fruit'=>'Orange'
);
$array[] = array(
'id'=>7,
'Fruit'=>'Banana'
);
$array =array(
array('Id'=>2, 'Fruit'=>'Apple'),
array('Id'=>5, 'Fruit'=>'Orange')
);
NB: array keys are case-sensitive.
Related
I have two array:
$arr1 = array(
'attributes' => array(
'fruit' => 'banana',
),
);
$arr2 = array(
'attributes' => array(
'color' => 'red',
),
);
$result = array_merge($arr1, $arr2);
The result is:
Array ( [attributes] => Array ( [color] => red ) )
But my expected result:
Array ( [attributes] => Array ( [color] => red [fruit] => banana ) )
What I am doing wrong? Should I use array_merge
or maybe will be better and easier just to use array_push and use only ('color' => 'red') ?
array_merge_recursive() is a great fit here.
$resultArray = array_merge_recursive($arr1, $arr2);
try this:
$result = array('attributes' => array_merge($arr1['attributes'], $arr2['attributes']));
print_r($result);
I am having array within array values as below.
Array
(
[0] => Array
(
[0] => Array
(
[Floor] => Floor-1
)
[1] => Array
(
[Flat] => Flat A2
)
[2] => Array
(
[Area] => Balcony,
)
)
)
I need to make it as single associative array as below.
Array
(
[0] => Array
(
[Floor] => Floor-1
[Flat] => Flat A2
[Area] => Balcony,
)
)
How can i do this ?
This example should help you.
<?php
$arr = array(
array(
'floor'=>'Floor-1'
),
array(
'Flat'=>'Flat A2'
),
array(
'Area'=>'Balcony,'
),
);
$final_array = array();
foreach ($arr as $arr1) {
foreach ($arr1 as $key => $value) {
$final_array[$key] = $value;
}
}
?>
Output will be
Array
(
[floor] => Floor-1
[Flat] => Flat A2
[Area] => Balcony,
)
Here we have created an empty array called as $final_array we will append this array by using foreach loop.
Remember, if you have a same array key then the last value will overwrite like below.
<?php
$arr = array(
array(
'floor'=>'Floor-1',
'floor'=>'Floor-2',
),
array(
'Flat'=>'Flat A2'
),
array(
'Area'=>'Balcony,'
),
array(
'Area'=>'Balcony2,'
),
);
$final_array = array();
foreach ($arr as $arr1) {
foreach ($arr1 as $key => $value) {
$final_array[$key] = $value;
}
}
?>
Now, output will be
Array
(
[floor] => Floor-2
[Flat] => Flat A2
[Area] => Balcony2,
)
<?php
$array = [
[
[
'foo' => 'big'
],
[
'bar' => 'fat'
],
[
'baz' => 'mamma'
]
]
];
$merged[0] = array_reduce($array[0], function($carry, $item) {
return array_merge((array) $carry, $item);
});
var_export($merged);
Output:
array (
0 =>
array (
'foo' => 'big',
'bar' => 'fat',
'baz' => 'mamma',
),
)
This single line code is enough to do this
$newArr = call_user_func_array('array_merge',$dataArr); ///where $dataArr is your array..
call_user_func_array will call a callback function with array of parameters and array_merge will merge all these parameters in single array read more about call_user_func_array() and array_merge()
Example code:
<?php
$dataArr = array(
array(
'Floor'=>'Floor-1'
),
array(
'Flat'=>'Flat A2'
),
array(
'Area'=>'Balcony,'
),
);
$newArr = call_user_func_array('array_merge',$dataArr);
echo "<pre>"; print_r($newArr);
?>
This will give you :
Array
(
[Floor] => Floor-1
[Flat] => Flat A2
[Area] => Balcony,
)
Suppose i have 2 arrays:
$array1 =( [0] => Array ( [user] => 'aaa' [count] => '123' )
[1] => Array ( [user] => 'bbb' [count] => '456' )
[2] => Array ( [user] => 'ccc' [count] => '789' ) );
$array2= ( [0] => aaa)
[1] => ccc );
I would like to search values from second array in first array and create the third array that will contain all elements from first array, like this:
$array3 =( [0] => Array ( [user] => 'aaa' [count] => '123' )
[1] => Array ( [user] => 'ccc' [count] => '789' ) );
Please help. Thank you in advance
(sorry for bad english)
$array1 = array(
array( 'user' => 'aaa', 'count' => 123 ),
array( 'user' => 'bbb', 'count' => 456 ),
array( 'user' => 'ccc', 'count' => 789 ),
);
$array2 = array('aaa', 'ccc');
var_dump(array_filter($array1, function($el) use($array2) {
return in_array($el['user'], $array2);
}));
It preserves keys, you can reset them if needed. Or with foreach loop
$out = array();
foreach($array1 as $el)
{
if (in_array($el['user'], $array2))
$out[] = $el;
}
var_dump($out);
Can try using foreach(). Example here...
$array3 = array();
foreach($array2 as $search){
foreach($array1 as $val){
if($search == $val['user']){
$array3[] = $val;
}
}
}
print_r($array3);
I have an array as such:
[0] => Array
(
[0] => 1
[1] => 30
[2] => 33
)
[1] => Array
(
[id] => 5
)
I want to move all values in the [0] index out so they become part of the parent array. So the final outcome would look like such:
[0] => Array
(
[id] => 1
)
[1] => Array
(
[id] => 30
)
[2] => Array
(
[id] => 33
)
[3] => Array
(
[id] => 5
)
As you can see the numerical indexes on [0] have now changed to id
I've tried using array_map('current', $array[0])
to no avail, any suggestions?
You could use the ol' trusty double foreach:
$new_array = array();
foreach ($array as $arr) {
foreach ($arr as $ar) {
$new_array[] = array('id'=>$ar);
}
}
Demo
$data = array(
array(1, 30, 33),
array('id' => 5)
);
$result = array();
array_walk_recursive(
$data,
function($value) use (&$result) {
$result[] = array('id' => $value);
}
);
var_dump($result);
Just to show that iterators can be really useful tools as well:
$data = array(
array(1, 30, 33),
array('id' => 5)
);
$result = array();
foreach (new RecursiveIteratorIterator(
new RecursiveArrayIterator($data),
RecursiveIteratorIterator::LEAVES_ONLY
) as $value) {
$result[] = array('id' => $value);
}
var_dump($result);
$array = [
[1, 30, 33],
['id' => 5]
];
$result = array_reduce($array, function (array $result, array $array) {
return array_merge($result, array_map(
function ($id) { return compact('id'); },
array_values($array)
));
}, []);
var_dump($result);
Admittedly not the simplest way to solve this, but very "functional". ;)
$array = array(
array(0 => 1,1 => 30,2 => 33,),
array("id" => 5,)
);
$result = array_merge(
array_map('array_flip',
array_chunk(
array_fill_keys($array[0], "id"),
1, true)
),
array_slice($array, 1)
);
var_export($result);
Results in:
array (
array ( 'id' => 1 ),
array ( 'id' => 30 ),
array ( 'id' => 33 ),
array ( 'id' => 5 )
)
I have an array like:
Array ( [id] => 1 [code] => FAC876 )
How do I push it into another array using PHP, such that the result is like:
Array ( [0] => Array ( [id] => 1 [code] => FAC876 )
[1] => Array ( [id] => 2 [code] => GEO980 )
)
Simply threat the array as any other variable.
So if this is what you got:
$array = array();
$array1 = array( "id"=>1, "code"=>"FAC876" );
$array2 = array( "id"=>2, "code"=>"GEO980" );
You could do either
$array[] = $array1;
$array[] = $array2;
or
$array[0] = $array1;
$array[1] = $array2;
or
$array = array($array1, $array2);
or
array_push($array, $array1);
array_push($array, $array2);
Any of those four possibilites will give you what you want.
You almost posted the answer yourself:
Array (
0 => Array ( 'id' => 1, 'code' => 'FAC876' ),
1 => Array ( 'id' => 2, 'code' => 'GEO980' )
)
$arr1 = array(
'id' => 1,
'code' => 'FAC876',
);
$arr2 = array(
$arr1,
array(
'id' => 2,
'code' => 'GEO980',
),
);
or
$arr1 = array(
'id' => 1,
'code' => 'FAC876',
);
$arr2 = array(
'id' => 2,
'code' => 'GEO980',
);
$arr3 = array($arr1, $arr2);
or lots of other ways to achieve that.
$ar=array();
$ar[]=array("no"=>10,"name"=>"abc");
$ar[]=array("no"=>20,"name"=>"pqr");
$arrays = array();
$array1 = array("id" => 1, "code" => "ABC");
$array2 = array("id" => 2, "code" => "DEF");
array_push($arrays, $array1, $array2);
$array = array(array( "id"=>1, "code"=>"FAC876" ) , array( "id"=>2, "code"=>"GEO980" ));
or
$array = array();
$array[] = array( "id"=>1, "code"=>"FAC876" );
$array[] = array( "id"=>2, "code"=>"GEO980" );
or
$array = array();
array_push($array, array( "id"=>1, "code"=>"FAC876" ));
array_push($array, array( "id"=>2, "code"=>"GEO980" ));