How to transform this php associative array? - php

I have this php associative input array.
array(
(int) 0 => array(
'Data' => array(
'id' => '12',
'type_id' => '1',
'data_value' => '35.5000'
),
'Type' => array(
'id' => '1',
'name' => 'Temperature'
)
),
(int) 1 => array(
'Data' => array(
'id' => '11',
'type_id' => '1',
'data_value' => '33.7000'
),
'Type' => array(
'id' => '1',
'name' => 'Temperature'
)
)
I want to convert it to this output array;
array(
(int) 0 => array(
(int) 0 => array(
'v' => (int) 1
),
(int) 1 => array(
'v' => '35.5000'
)
),
(int) 1 => array(
(int) 0 => array(
'v' => (int) 2
),
(int) 1 => array(
'v' => '33.7000'
)
)
The element data_value is extracted from the input array into the output array.
The code I have written looks like this;
$data2 = array();
foreach ($InputArray as $key=>$value)
{
$data2[] = array(
array(
array('v' => $key),
array('v' => $value['Data']['data_value'])
)
);
}
Unfortunately, this code does not work. The output from this code looks like this;
array(
(int) 0 => array(
(int) 0 => array(
(int) 0 => array(
[maximum depth reached]
),
(int) 1 => array(
[maximum depth reached]
)
)
),
(int) 1 => array(
(int) 0 => array(
(int) 0 => array(
[maximum depth reached]
),
(int) 1 => array(
[maximum depth reached]
)
)
)
What did I do wrong? Why do I get the error "maximum depth reached"? How can I retrieve the desired output array? I am actually doing this in cakephp.
Thank you very much for any help.

That is one wrapping array() to many:
$data2 = array();
foreach ($InputArray as $key=>$value)
{
$data2[] = array(
array('v' => $key),
array('v' => $value['Data']['data_value'])
);
}
You can see this working here. It is good use to prepare your code in services like ideome or plnkr in order to make it easier for people to help you with debugging.

As far as I know, the problem is related to a setting for PHP http://www.hardened-php.net/suhosin/configuration.html#suhosin.executor.max_depth . Could you check with your hosting provider?

Related

How to merge multiple dimension array cakephp

I just using function query() of cakePhp. The query will return array something like :
array(
(int) 0 => array(
'cate' => array(
'date' => '2016-12-05',
),
'cate_detail' => array(
'rel_data_category' => '11'
),
'cate_item' => array(
'price' => '150.000'
)
),
(int) 1 => array(
'cate' => array(
'date' => '2016-12-05',
),
'cate_detail' => array(
'rel_data_category' => '10'
),
'cate_item' => array(
'price' => '250.000'
)
),
(int) 2 => array(
'cate' => array(
'date' => '2016-12-06',
),
'cate_detail' => array(
'rel_data_category' => '10'
),
'cate_item' => array(
'price' => '250.000'
)
)
)
Now, I want to check if array have the same cate.date will merge array (in this case is elements 0,1 of my array). Output something like :
array(
(int) 0 => array(
'cate' => array(
'date' => '2016-12-05',
),
'cate_detail' => array(
(int) 0 => array (
'rel_data_category' => '11',
'price' => '150.000'
),
(int) 1 => array(
'rel_data_category' => '10',
'price' => '250.000'
)
)
),
(int) 1 => array(
'cate' => array(
'date' => '2016-12-06',
),
'cate_detail' => array(
(int) 0 => array (
'rel_data_category' => '10'
'price' => '250.000'
)
)
)
)
Please help!
You will need to loop through the results and build a new array with the data in the form you want. You can use the CakePHP Hash::extract() method to map the dates to indexes so that you can combine the data for the dates.
For example:-
// Get out all the dates available and then flip the array so that we have a map of dates to indexes
$dates = Hash::extract($results, '{n}.cate.date');
$datesMap = array_flip($dates);
// Define an empty array that we will store the new merged data in
$data = [];
// Loop through the query results and write to the $data array using the map of dates
foreach ($results as $result) {
$key = $datesMap[$result['cate']['date']];
$data[$key]['cate'] = $result['cate'];
$data[$key]['cate_detail'][] = [
'rel_data_category' => $result['cate_detail']['rel_data_category'],
'price' => $result['cate_item']['price']
];
}

php insert key/value into associative array

I'm trying to insert a couple of new Key/Value pairs into an associative array at a specific place. From other reading I've done on SO, I'm pretty sure I have to loop through the array and insert the new values when a condition is set.
Here is the current array
array(
(int) 0 => array(
'Product' => array(
'id' => '59',
'title' => ' Blue Dress',
'Review' => array(
'id' => '7',
'product_id' => '59',
'Review' => array(
(int) 0 => array(
'average' => '3.0000'
)
)
)
)
)
(int) 1 => array(
'Product' => array(
'id' => '60',
'title' => 'Red Dress',
'Review' => array()
)
)
)
The key Review does not always have data, but when it does I want to insert a new key-value similar to the following excerpt
(int) 0 => array(
'Product' => array(
'id' => '59',
'title' => ' Blue Dress',
'Review' => array(
'id' => '7',
'product_id' => '59',
'Review' => array(
(int) 0 => array(
'average' => '3.0000'
'some_value' => '5'
)
)
)
)
)
I've tried a few things without success.
Any help is much appreciated thanks.
You can do something like this:
if(!empty($your_array[index]['Product']['Review'])){
$your_array[index]['Product']['Review']['Review'][index]['some_value'] = 'new_value';
}
In your example it could be:
if(!empty($your_array[0]['Product']['Review'])){
$your_array[0]['Product']['Review']['Review'][0]['some_value'] = 'new_value';
}
Again, you didn't mention your code. So, it's hard to figure out what you want exactly!
You should iterate through Your array and pass current value be reference:
// Notice & sign before variable
foreach ($data as &$product)
{
if ($product['Product']['Review'])
{
// or iterate through Review array
$product['Product']['Review']['Review'][0]['some_value'] = 5;
}
}

How to copy this portion of the array to a new array in php?

I have this php array X.
X= array(
'Parent' => array(
'title' => '123',
)
)
I have this php array Y.
Y = array(
'Parent' => array(
'id' => '16',
'title' => 'T1',
),
'Children' => array(
(int) 0 => array(
'id' => '8',
'serial_no' => '1',
),
(int) 1 => array(
'id' => '9',
'serial_no' => '2',
),
(int) 2 => array(
'id' => '14',
'serial_no' => '6',
)
)
)
I want to copy the Children of array Y to the parent of array X to form array Z such that it looks like this;
Z= array(
'Parent' => array(
'title' => '123',
)
'Children' => array(
(int) 0 => array(
'serial_no' => '1'
),
(int) 1 => array(
'serial_no' => '2'
),
(int) 2 => array(
'serial_no' => '6'
)
)
)
Please note that the id key-value pair was removed from the Children of array Y.
I wrote some code of my own.
$Z = array();
$i=0;
foreach($Y as $temp)
{
$Z['Children'][$i] = $temp['Children'][$i];
unset($Z['Children'][$i]['id'];
$i++;
}
$Z['Parent']=$temp['Parent'];
Unfortunately, there is an undefined index error. How can this be done in php? Forget about my code if there are better approaches.
Actually your approach works too, but you need to iterate over sub-array:
$Z = array();
$i=0;
foreach($Y['Children'] as $temp)
{
$Z['Children'][$i] = $temp;
unset($Z['Children'][$i]['id'];
$i++;
}
or what I may do:
$Z = $X;
$Z['Children'] = array();
foreach ( $Y['Children'] as $child ) {
$Z['Children'][] = array(
'serial_no' => $child['serial_no'],
);
}
You can do like.
$Z = array();
foreach($Y['Children'] as $temp)
{
$Z['Children'][] = array('serial_no' => $temp['serial_no']);
}
$Z['Parent']=$X['Parent'];

How to remove the unnecessary information and restructure this associative array in php?

I have this php associative array which I created from a MySQL query. It looks like this;
array(
(int) 0 => array(
'items' => array(
'index_no' => '1'
),
(int) 0 => array(
'NumItems' => '2'
)
),
(int) 1 => array(
'items' => array(
'index_no' => '2'
),
(int) 0 => array(
'NumItems' => '3'
)
)
It looks unnecessarily complicated. I would like to simplify it to look something like this;
array(
(int) 0 => array(
'index_no' => '1',
'NumItems' => '2'
)
),
(int) 1 => array(
'index_no' => '2',
'NumItems' => '3'
)
)
How can this be done in php? I have been stuck on this problem for some time. I will post my answer if I have it. I would appreciate it if someone could give me some starting point. Thank you very much.
You can try this out:
$tempArray = array(
(int) 0 => array(
'items' => array(
'index_no' => '1'
),
(int) 0 => array(
'NumItems' => '2'
)
),
(int) 1 => array(
'items' => array(
'index_no' => '2'
),
(int) 0 => array(
'NumItems' => '3'
)
));
$newArray = array();
$i=0;
foreach($tempArray as $temp) {
$newArray[$i]['index_no'] = $temp['items']['index_no'];
$newArray[$i]['NumItems'] = $temp[0]['NumItems'];
$i++;
}
print "<pre>";
print_r($newArray);
try this
<?php $res=array(array('item'=>1,'number'=>5),array('item'=>2,'number'=>56));
$final_array =array();
$i=0;
foreach ($res as $val)
{
foreach($val as $key=>$val2)
{
$final_array[$i][$key] = $val2;
}$i++;
}
print_r($final_array);
?>
Here is solution for you.
$diffArray = array(
(int) 0 => array(
'items' => array(
'index_no' => '1'
),
(int) 0 => array(
'NumItems' => '2'
)
),
(int) 1 => array(
'items' => array(
'index_no' => '2'
),
(int) 0 => array(
'NumItems' => '3'
)
));
print_r($diffArray);
$getArray = array();
foreach ($diffArray as $simArray) {
$getArray['index_no'][] = $simArray['items']['index_no'];
$getArray['NumItems'][]= $simArray[0]['NumItems'];
}
print_r($getArray);
$newArray = array();
foreach ($array as $items) {
$temp = array('index_no' => $items['index_no']);
$temp = array_merge($temp, $items[0]);
$newArray[] = $temp;
}
it will add all the keys to the array under index - 0

Push array inside another array

I have a site develop in php and I have a function where I want to create an array inside other array.
My query are this (i'm using cakephp but in this case is only a problem of array don't tell me to use contain or something like that from cakephp because is a large query and I need to construct my query and my array in this mode)
My array $product_ingredient contain some value.
foreach ($product_ingredient as $key) {
$ingredient_level1 = $this->ProductIngredientVersion->query('SELECT * FROM ingredients_ingredients
WHERE ingredients_ingredients.ingredient_id = :ingredient_id
AND ingredients_ingredients.product_id = :id
AND ingredients_ingredients.version_id = :version_id
AND ingredients_ingredients.level = 1', array('id' => $id, 'ingredient_id' => $key['products_ingredients']['ingredient_id'], 'version_id' => $key['products_ingredients']['version_id']));
foreach($ingredient_level1 as $key2){
$ingredient_level2 = array($this->ProductIngredientVersion->query('SELECT * FROM ingredients_ingredients
WHERE ingredients_ingredients.ingredient_id = :ingredient_id
AND ingredients_ingredients.product_id = :id
AND ingredients_ingredients.version_id = :version_id
AND ingredients_ingredients.level = 2', array('id' => $id, 'ingredient_id' => $key2['ingredients_ingredients']['ingredient2_id'], 'version_id' => $key2['ingredients_ingredients']['version_id'])));
array_push($ingredient_level1, $ingredient_level2);
}
array_push($ingredient_ingredient, $ingredient_level1);
}
The result is that:
array(
(int) 0 => array(
(int) 0 => array(
'ingredients_ingredients' => array(
'id' => '34',
'level' => '1'
)
),
(int) 1 => array(
(int) 0 => array(
(int) 0 => array(
'ingredients_ingredients' => array(
'id' => '35',
'level' => '2'
)
)
)
)
)
But I would like this result
array(
(int) 0 => array(
(int) 0 => array(
'ingredients_ingredients' => array(
'id' => '34',
'level' => '1',
array(
'ingredients_ingredients' => array(
'id' => '35',
'level' => '2'
)
)
)
)
How can I solve?
Replace
array_push($ingredient_ingredient, $ingredient_level1);
by
array_push(
$ingredient_ingredient[0][0]['ingredients_ingredients'],
$ingredient_level1
);
This pushes the array to the appropriate cascade level. However it is very likely that this is the most efficient way, if you're unable to modify SQL or array structure elsewhere.

Categories