I am trying to rebuild php array, which I can get with one query from database.
To build lists and add items I need the array to be grouped and rebuild.
From query I receive array like this:
array (
0 =>
array (
'item_id' => '1',
'item_name' => 'aaa',
'group_id' => '7',
'group_name' => 'first'
),
1 =>
array (
'item_id' => '2',
'item_name' => 'bbb',
'group_id' => '7',
'group_name' => 'first'
),
2 =>
array (
'item_id' => '3',
'item_name' => 'ccc',
'group_id' => '9',
'group_name' => 'second'
),
3 =>
array (
'item_id' => '4',
'item_name' => 'ddd',
'group_id' => '9',
'group_name' => 'second'
)
);
I need to rearrange this array to following, which I can use for js parsing:
array(
array ('group_id' => '7', 'group_name' => 'first', 'items' => array (
0 => array (
'item_id' => '1',
'item_name' => 'aaa',
),
1 => array (
'item_id' => '2',
'item_name' => 'bbb',
)
)
),
array ('id' => '8', 'name' => 'second', 'items' => array (
0 =>
array (
'item_id' => '3',
'item_name' => 'ccc',
),
1 =>
array (
'item_id' => '4',
'item_name' => 'ddd',
)
)
)
);
I have stuck with following code:
function rebuild($groupby, $param, $data) {
$newarray = array();
foreach($data as $key => $val) {
if(array_key_exists($groupby, $val)){
$newarray[$val[$groupby]] = $val[$param];
$newarray['items'][] = $val;
}else{
$newarray[""][] = $val;
}
}
return $newarray;
}
$show = rebuild('group_id', 'group_name', $data);
echo "<pre>" . var_export($show, true) . "</pre>";
Any advise would appreciated.
I always like to use the unique identifier (here, the group ID), as array key in the resulting array - that makes it easier to associate the right items to the right new entry, without having to “search” the new array for the entry with the matching identifier.
$newarray = array();
foreach($data as $item) {
$newarray[$item['group_id']]['group_id'] = $item['group_id'];
$newarray[$item['group_id']]['group_name'] = $item['group_name'];
$newarray[$item['group_id']]['items'][] = [
'item_id' => $item['item_id'],
'item_name' => $item['item_name']
];
}
$newarray = array_values($newarray); // resets the array keys to a zero-based index
var_dump($newarray);
group_id and group_name get overwritten in the result array elements all the time - but since we access the correct entry by the group id already, that does not matter. They will always be “overwritten” with the values they already have, so that does no harm.
And item_id and item_name simply get added to the items array of the entry identified by the group id.
Related
Hello i am trying to create data set like
Expected output:-
Array
(
[0] => Array
(
[sku] => sku
[variant_option_one_name] => Color
[variant_option_one_value] => Cyan
),
[1] => Array
(
[sku] => sku
[variant_option_one_name] => Color
[variant_option_one_value] => Red
)
)
but i am not sure what is missing in code.
Here is the code
$array = array(
0 => array(
'id_product_attribute' => '17615',
'id_product' => '2295',
'reference' => '',
'available_date' => '0000-00-00',
'vend_id' => null,
'id_shop' => '1',
'id_attribute_group' => '1',
'is_color_group' => '1',
'group_name' => 'Color',
'attribute_name' => 'Cyan',
'id_attribute' => '1',
),
1 => array(
'id_product_attribute' => '17616',
'id_product' => '2295',
'reference' => '',
'available_date' => '0000-00-00',
'vend_id' => null,
'id_shop' => '1',
'id_attribute_group' => '1',
'is_color_group' => '1',
'group_name' => 'Color',
'attribute_name' => 'Red',
'id_attribute' => '21',
),
);
$ids = array();
foreach ($array as $combinations) {
$ids['sku'] = 'sku';
$ids['variant_option_one_name'] = $combinations['group_name'];
$ids['variant_option_one_value'] = $combinations['attribute_name'];
}
print_r($ids);//
Here i am getting
Array
(
[sku] => sku
[variant_option_one_name] => Color
[variant_option_one_value] => Red
)
The above output i am getting. Seems like data is overwrite
Any correction to get both the data ?
I do not get both the colors in array. It
Thankyou
You're absolutely right, the values are being overwritten each time.
What you need to do is, each time you loop you should create a new array containing your values, and then assign that array to a new index inside the main array (so you get an array of arrays, like the expected output you've shown):
foreach ($array as $combinations) {
$arr = array();
$arr['sku'] = 'sku';
$arr['variant_option_one_name'] = $combinations['group_name'];
$arr['variant_option_one_value'] = $combinations['attribute_name'];
$ids[] = $arr;
}
Live Demo: http://sandbox.onlinephpfunctions.com/code/3a0cf7f8cbb994ef4192c1e23493bef397785937
foreach ($array as $combinations) {
array_push ($ids, [
'sku' => 'sku',
'variant_option_one_name' => $combinations['group_name'],
'variant_option_one_value' => $combinations['attribute_name']
]);
}
please find below contains array in php
$data = array ( 'questions' => array ( 1 =>
array (
'section_id' => '61',
'questionid' => '2035',
'time_spent' => '0',
),
2 =>
array (
'section_id' => '61',
'questionid' => '2036',
'time_spent' => '0',
),
3 =>
array (
'section_id' => '61',
'questionid' => '2037',
'time_spent' => '0',
),
),)
how can i access section_id, and time_spent data from array
Please use the below code : Also see this link
foreach($data as $key => $values){
foreach ($values as $ikey => $secArr) {
# code...
$section_id = $secArr['section_id'];
$time_spent = $secArr['time_spent'];
echo 'Section Id: '.$section_id.' | Time Spend:'.$time_spent.'</br>';
}
}
I hope it will helps you.
$array1 = array(
[0] => array(
'id' => 'gdye6378399sjwui39',
'name' => 'Plate 1'
),
[1] => array(
'id' => 'xyz6378399sjwui39',
'name' => 'Plate 2'
),
[2] => array(
'id' => 'tr2e6378399sjwui39',
'name' => 'Plate 3'
)
)
and another array
$array2 = array(
[0] => array(
'id' => 'gdye6378399sjwui39',
'ETA' => '8'
),
[1] => array(
'id' => 'tr2e6378399sjwui39',
'ETA' => '9'
)
[2] => array(
'id' => 'xyz6378399sjwui39',
'ETA' => '5'
)
)
I want to compare the two arrays. I am doing it like this way:-
if(!empty($array2))
{
foreach($array1 as $ck => $cl)
{
foreach($array12 as $ued){
if($cl['id'] == $ued['id'])
{
$array1[$ck]['ETA'] = $ued['ETA'];
break;
}
}
}
What are the other better ways to do this? The order of the two arrays may vary, and so does the size.
If you index the second array by the id (using array_column()) you can get away without using the inner foreach() and just use isset()...
$match = array_column($array2, null, 'id');
foreach ( $array1 as $ck=>$cl) {
if ( isset($match[$cl['id']]) ) {
$array1[$ck]['ETA'] = $match[$cl['id']]['ETA'];
}
}
print_r($array1);
I have a multi-dimensional array in the following format:
[0] = (
'id' => '1',
'type' => 'fish',
'owner' => 'bob',
)
[1] = (
'id' => '2',
'type' => 'cat',
'owner' => 'mary',
)
[2] = (
'id' => '3',
'type' => 'dog',
'owner' => 'larry',
)
[3] = (
'id' => '2',
'type' => 'cat',
'owner' => 'fred',
)
I would like to search for a value, and they return an array that contains all keys from matching arrays and looks like this on a search for type=cat:
[0] = (
'id' => '2',
'type' => 'cat',
'owner' => 'mary',
)
[1] = (
'id' => '2',
'type' => 'cat',
'owner' => 'fred',
)
I know I'm trying to treat the array as a database, but in this case it's dynamic data that doesn't need to be stored once the program ends.
Any advice?
Loop through the array:
function loopAndFind($array, $index, $search){
$returnArray = array();
foreach($array as $k=>$v){
if($v[$index] == $search){
$returnArray[] = $v;
}
}
return $returnArray;
}
//use it:
$newArray = loopAndFind($oldArray, 'type', 'cat');
you must iterate array like:
foreach ($array as $i => $values) {
print "$i {\n";
foreach ($values as $key => $value) {
print " $key => $value\n";
}
print "}\n";
}
and then check for 'type' key value....
then record which is matched must copy it to new array...
I'm having a problem lately that's driving me crazy. I have a multi-dimensional array like this:
$a = array(
'db' => array(
'0' => array(
'id' => '1',
'name' => 'test',
'cat' => array(
'a' => '15',
'b' => '20',
'c' => '30'
),
'canvas' => '2'
),
'1' => array(
'id' => '2',
'name' => 'test2',
'cat' => array(
'a' => '15',
'b' => '20',
'c' => '30'
),
'canvas' => '2'
)
'2' => array(
'id' => '3',
'name' => 'test',
'cat' => array(
'a' => '50',
'b' => '40',
'c' => '90'
),
'canvas' => '1'
)
)
);
And i want to search on it using a function like this: search('canvas = 1');
That would return all the arrays, child of db, that have a key canvas with the value of 1. Or, for example:
search('a = 15');
Would return all arrays that have a key, child of cat, named a and with a value of 15.
$a = array(
'db' => array(
'0' => array(
'id' => '1',
'name' => 'test',
'cat' => array(
'a' => '15',
'b' => '20',
'c' => '30'
),
'canvas' => '2'
),
'1' => array(
'id' => '2',
'name' => 'test2',
'cat' => array(
'a' => '15',
'b' => '20',
'c' => '30'
),
'canvas' => '2'
),
)
);
//checks if array $array contains element with $searchKey key, and $searchVal value
function arrayContains($array, $searchVal, $searchKey) {
if (!is_array($array))
return false;
foreach ($array as $key => $value) {
if ($key === $searchKey && $searchVal === $value)
return true;
if (is_array($value) && arrayContains($value, $searchVal, $searchKey))
return true;
}
return false;
}
function search($a, $search) {
list($searchKey, $searchVal) = explode('=', $search);
$result = array();
foreach($a as $val) {
if (arrayContains($val, $searchVal, $searchKey))
$result[] = $val;
}
return $result;
}
print_r(search($a['db'], "a=15"));
print_r(search($a['db'], "canvas=1"));
Which produces this output(outputs sub-arrays of $a['db'] which contain searched key=>value pair):
Array
(
[0] => Array
(
[id] => 1
[name] => test
[cat] => Array
(
[a] => 15
[b] => 20
[c] => 30
)
[canvas] => 2
)
[1] => Array
(
[id] => 2
[name] => test2
[cat] => Array
(
[a] => 15
[b] => 20
[c] => 30
)
[canvas] => 2
)
)
Array
(
[0] => Array
(
[id] => 3
[name] => test
[cat] => Array
(
[a] => 50
[b] => 40
[c] => 90
)
[canvas] => 1
)
)
Just check the below link if this can help you -
http://php.net/manual/en/function.array-search.php
It contains detailed documentation of php function array_search() and various user codes for searching in multi-dimensional array along with user reviews.
function search($array, $canvas)
{
$result = array();
foreach ($array as $k1 => $v1) {
foreach ($v1 as $k2 => $v2) {
if ($v2['canvas'] == $canvas) {
$result[] = $array[$k1][$k2];
}
}
}
return $result;
}
// $a = your array
print_r(search($a, 1));