This single line would do that:
$array = array_column($array, 'plan');
The first argument is an array | The second argument is an array key.
For details, go to official documentation: https://www.php.net/manual/en/function.array-column.php.
Answer from Usman Ahmed on Stack OverflowThis single line would do that:
$array = array_column($array, 'plan');
The first argument is an array | The second argument is an array key.
For details, go to official documentation: https://www.php.net/manual/en/function.array-column.php.
Assuming this array may or may not be redundantly nested and you're unsure of how deep it goes, this should flatten it for you:
function array_flatten($array) {
if (!is_array($array)) {
return FALSE;
}
$result = array();
foreach ($array as
value) {
if (is_array($value)) {
$result = array_merge($result, array_flatten($value));
}
else {
$result[
value;
}
}
return $result;
}
A quick way to do this is:
$obj = json_decode(json_encode($array));
Explanation
json_encode($array) will convert the entire multi-dimensional array to a JSON string. (php.net/json_encode)
json_decode($string) will convert the JSON string to a stdClass object. If you pass in TRUE as a second argument to json_decode, you'll get an associative array back. (php.net/json_decode)
I don't think the performance here vs recursively going through the array and converting everything is very noticeable, although I'd like to see some benchmarks of this. It works, and it's not going to go away.
The best way would be to manage your data structure as an object from the start if you have the ability:
$a = (object) array( ... ); $a->prop = $value; //and so on
But the quickest way would be the approach supplied by @CharlieS, using json_decode(json_encode($a)).
You could also run the array through a recursive function to accomplish the same. I have not benchmarked this against the json approach but:
function convert_array_to_obj_recursive($a) {
if (is_array($a) ) {
foreach(
k => $v) {
if (is_integer($k)) {
// only need this if you want to keep the array indexes separate
// from the object notation: eg.
a['index'][$k] = convert_array_to_obj_recursive($v);
}
else {
k] = convert_array_to_obj_recursive($v);
}
}
return (object) $a;
}
// else maintain the type of $a
return $a;
}
Hope that helps.
EDIT: json_encode + json_decode will create an object as desired. But, if the array was numerical or mixed indexes (eg. array('a', 'b', 'foo'=>'bar') ), you will not be able to reference the numerical indexes with object notation (eg. o[1]). the above function places all the numerical indexes into the 'index' property, which is itself a numerical array. so, you would then be able to do
$o->index[1]. This keeps the distinction of a converted array from a created object and leaves the option to merge objects that may have numerical properties.
Simple use array_column() function returns the values from a single column in the input array
$new_arr = array_column($array,'sku');
print_r($new_arr);
Use array_map() or array_column()
// (PHP 4 >= 4.0.6, PHP 5, PHP 7)
print_r(array_map(function($a){return $a['sku'];}, $array));
// (PHP 5 >= 5.5.0, PHP 7)
print_r(array_column($array,'sku'));
you new array will be
$new_array = array_map(function($a){return $a['sku'];}, $array);
$new_array = array_column($array,'sku');
For example:
$new_array = array_map(
function ($v) { return [$v]; },
['A', 'B', 'C']
);
This is a blanket statement on how to get your desired array :
$desired_array = array(array("0"=>"A"), array("0"=>"B"), array("0"=>"C"));
However, dynamically, you could do the following :
//Assume $original_array = array("0"=>"A", "1"=>"B", "2"=>"C");
$desired_array = array(); // New Array
for($i = 0; $i < count($original_array); $i++){ // Loop over all elements in original array
array_push($desired_array, array("0"=>$original_array[$i])); // Place each valueable as an array in new desired array
}
Flip the value and remove the old one in the same array... this should be fine provided the created value doesn't overwrite one of the existing entries, which I highly doubt since created seems to be a timestamp.
foreach($myArray as $index => $entry) {
$myArray[$entry['created']] = $entry;
unset($myArray[$index]);
}
Or you could keep both copies and use references to save on ram.
foreach($myArray as &$entry)
$myArray[$entry['created']] =& $entry;
i don't know what you realy want to do .... maybe this could you help
<?php
$new = array();
foreach($oldArr as $arr) {
$new[$arr['created']] = $arr;
}
print_r($new);
?>
I think you can use array_reduce() function. For example:
$multi= array(0 => array('t1' => 'test1'),1 => array('t2' => 'test2'),2 => array('t3' => 'test3'),3 => array('t4' => 'test4'));
$single= array_reduce($multi, 'array_merge', array());
print_r($single); //Outputs the reduced aray
You can use as follows :
$newArray = array();
foreach($arrayData as $key => $value) {
foreach($value as $key2 => $value2) {
$newArray[$key2] = $value2;
}
}
Where $arrayData is your DB data array and $newArray will be the result.