Accessing multi php array values - php

Hi I'm trying to access elements in array.
array(
(int) 0 => array(
'requests_users' => array(
'request_id' => '1'
),
(int) 0 => array(
'ct' => '2'
)
),
(int) 1 => array(
'requests_users' => array(
'request_id' => '2'
),
(int) 0 => array(
'ct' => '1'
)
),
(int) 2 => array(
'requests_users' => array(
'request_id' => '4'
),
(int) 0 => array(
'ct' => '2'
)
),
(int) 3 => array(
'requests_users' => array(
'request_id' => '5'
),
(int) 0 => array(
'ct' => '2'
)
)
)
By using for loop (under)
for($row=0;$row<count($list);$row++){
echo $list[$row]['requests_users']['request_id'];
}
I could get request_id values.
However, I'm having a trouble getting a 'ct' values.
Can you guys help me how to print 'ct' values?

how about like this..
for($row=0;$row<count($list);$row++){
echo $list[$row]['requests_users']['request_id'];
echo '<br/>';
echo $list[$row][0]['ct'];
}
try this.
should work

You can get "ct" value :
for($row=0;$row<count($list);$row++){
echo $list[$row][0]['ct'];
}

You can try like below code
for($row=0;$row<count($list);$row++){
echo $list[$row]['requests_users']['request_id'];
echo "<br>";
echo $list[$row][0]['ct'];
echo "<br><br>";
}

To access an elements of above array you can use following recursive function:
public function _convertToString($data){
foreach($data as $key => $value){
if(is_array($value)){
$this->_convertToString($value);
}else{
echo $key .'->'.$value;
}
}
}
you can call above function in following way:
$str = array(
"data" => "check",
"test1" => array(
"data" => "Hello",
"test3" => array(
"data" => "satish"
)
),
"test2" => array(
"data" => "world"
)
);
$this->_convertToString($str);
You can modify output or recursive function to meet your requirements. Can you add original array in your question, I mean not var_dump() so that I can use that directly and modify code in my answer, if needed.

Related

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

How to transform this php associative array?

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?

Merge subarrays to form an indexed array with first level removed [duplicate]

This question already has answers here:
How to Flatten a Multidimensional Array?
(31 answers)
Closed 2 years ago.
I have a problem. Maybe it will be simple to solve.
There is an array looking like this:
(int) 0 => array(
(int) 0 => array(
'Post' => array(
'name' => 'value'
)
)
),
(int) 1 => array(
(int) 0 => array(
'Post' => array(
'name' => 'value'
)
),
(int) 1 => array(
'Post' => array(
'name' => 'value'
)
)
)
That needs to look like this:
(int) 0 => array(
'Post' => array(
'name' => 'value'
)
)
(int) 1 => array(
'Post' => array(
'name' => 'value'
)
)
(int) 2 => array(
'Post' => array(
'name' => 'value'
)
)
I tried array_shift() and directly after that, array_values(), but that gave me only the first post.
I assume the order was "0, 0, 1", so PHP cut it after the first one.
$array = call_user_func_array('array_merge', $array);
This is what I have tried ..
<?php
$test = array(
array(array('post'=>array('name','value'))),
array(array('post'=>array('name','value')),array('post'=>array('name','value')),),
);
print_r($test);
$test_arr = array();
foreach($test as $value)
{
foreach($value as $subvalue)
{
$test_arr[] = $subvalue;
}
}
print_r($test_arr);
?>

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