i have the following array i obtain after running a var_dump($response).
array (size=2)
'count' => int 3
'data' =>
array (size=3)
0 => array (size=38)
'ref' => string '24750.0.2530' (length=12)
1 => array (size=38)
'ref' => string '24450.0.2530' (length=12)
i would like to display the 'ref' from the above array.tried running a foreach as below but i get a notice
Trying to get property of non-object
foreach($response as $object)
{
var_dump($object->ref);
}
foreach ($response['data'] as $data) {
var_dump($data['ref']);
}
That's because it isn't an object. It is an array of arrays. Try:
foreach ($response['data'] as $data) {
var_dump($data['ref']);
}
Related
my question is how can i get all values from determined level of some array, i have this array:
array (size=4)
'Azul' =>
array (size=2)
'128GB' =>
array (size=2)
'Cristal' => string 'Cristal' (length=7)
'Plastico' => string 'Plastico' (length=8)
'64GB' =>
array (size=1)
'Cristal' => string 'Cristal' (length=7)
'Blanco' =>
array (size=2)
'32GB' =>
array (size=1)
'Plastico' => string 'Plastico' (length=8)
'64GB' =>
array (size=1)
'Madera' => string 'Madera' (length=6)
'Dorado' =>
array (size=1)
'64GB' =>
array (size=1)
'Plastico' => string 'Plastico' (length=8)
'Verde' =>
array (size=1)
'64GB' =>
array (size=1)
'Madera' => string 'Madera' (length=6)
And i want get the first level with this recursive function, but i cant find more deeper than 2 levels for example i need the first level and i get:
Azul, Blanco, Dorado, Verde
But i need the second level of Azul i get:
128GB, 64GB
The questions is, if i need the 3rd level of Azul and 64GB, what can i do to get this, having the keys Azul and 64GB or level 3.
My recursive but buggy function is this:
function recursive($array, $level, $itemLVL)
{
foreach ($array as $key => $value) {
//If $value is an array.
if (is_array($value)) {
//We need to loop through it.
if ($level == $itemLVL) {
//echo "<br> Key: $key - Nivel:$level $itemLVL";
echo "<option value='$key'>$key</option>";
}
recursive($value, $level + 1, $itemLVL);
} elseif ($level == $itemLVL) {
echo "<option value='$key'>$key</option>";
}
}
}
If you know the key names and order, then something like this will return what is under those keys:
function get_array_path($path, $array) {
$temp =& $array;
foreach($path as $key) {
$temp =& $temp[$key];
}
return $temp;
}
Pass an array of the keys in order:
$result = get_array_path(['Azul', '64GB'], $array);
If you just want the keys under the path and not everything else, then pass false as the third argument:
function get_array_path($path, $array, $values=true) {
$temp =& $array;
foreach($path as $key) {
$temp =& $temp[$key];
}
if($values === false) {
$temp = array_keys($temp);
}
return $temp;
}
What I am trying to get is to remove duplicate values in the Rest field, but I want to assign / keep its date in the original. element:
array (size=413)
0 =>
array (size=5)
'Date' =>
array (size=1)
0 => int 1588520980
'Rest' => 123abc
1 =>
array (size=5)
'Date' =>
array (size=1)
0 => int 1588520981
'Rest' => qwe123
2 =>
array (size=5)
'Date' =>
array (size=1)
0 => int 1588520983
'Rest' => qwe123
I try it but it doesn't work
public function find_repeats($arr)
{
foreach(array_column($arr, 'Rest') as $ckey=>$value) {
$keys = array_reverse(array_keys(array_column($arr, 'Rest'), $value));
foreach ($keys as $v) {
if ($ckey != $v && isset($arr[$v]))
{
$arr[$ckey]['Date'][] = $arr[$v]['Date'][0];
unset($arr[$v]);
}
}
}
return $arr;
}
This is what the table should look like after this operation
array (size=413)
0 =>
array (size=5)
'Date' =>
array (size=1)
0 => int 1588520980
'Rest' => 123abc
1 =>
array (size=5)
'Date' =>
array (size=1)
0 => int 1588520981
1 => int 1588520983
'Rest' => qwe123
Thanks for help! :)
Simple solution without all these stacked functions:
$newData = [];
foreach ($arr as $item) {
$rest = $item['Rest'];
if (!isset($newData[$rest])) {
$newData[$rest] = $item;
} else {
$newData[$rest]['Date'][] = $item['Date'][0];
}
}
// optionally apply array_values to get 0-indexed array:
$newData = array_values($newData);
Below is my function:
public function getChildrenId(){
$child_id = array($this->db->query("SELECT customer_id
FROM " . DB_PREFIX . "customer
WHERE parent IN ( " .(int)$this->customer->getId().") "));
foreach($child_id as $id =>$value) {
$conv = json_decode(json_encode($value), true);
$final = array_slice($conv,2);
foreach ($final as $gchildren => $key) {
sort($key);
$gr = array_slice($key,0,$this->INF);
}
}
return $gr;
}
It outputs:
array (size=3)
0 =>
array (size=1)
'customer_id' => string '2' (length=1)
1 =>
array (size=1)
'customer_id' => string '4' (length=1)
2 =>
array (size=1)
'customer_id' => string '7' (length=1)
I am trying to get the values of the nested arrays. When I use foreach I only get data from array[0]. I also tried slicing the parent array and still didn't get it right, it outputs array,array,array.
I would like to extract these arrays values to a new array that I can use to query the database. final_array = array (2,4,7).
Thank you in advance!
If your array looks like this, then the foreach should create the array your looking for.
array (size=3)
0 =>
array (size=1)
'customer_id' => string '2' (length=1)
1 =>
array (size=1)
'customer_id' => string '4' (length=1)
2 =>
array (size=1)
'customer_id' => string '7' (length=1)
The following php will output array(2,4,7);
<?php
$aNewArray = array();
foreach($aArray as $aArray){
$aNewArray[] = $aArray['customer_id'];
}
var_dump($aNewArray);
?>
You dont need a multidimensional array for this though.
I am getting an array from table questions
$questions = Category::where('slug', $slug)->first()->questions->toArray();
then using foreach I iterate over array and push an array element during each iteration like below
foreach ($questions as $question) {
$question['options'] = Option::where('question_id', $question['id'])->get()->toArray();
}
Ideally I should get response like below
array (size=5)
0 =>
array (size=6)
'id' => int 1
'category_id' => int 1
'questions' => string 'The ozone layer restricts' (length=25)
'answer_id' => int 4
'image_path' => string '' (length=0)
'options' =>
array (size=4)
0 =>
array (size=3)
...
1 =>
array (size=3)
...
2 =>
array (size=3)
...
3 =>
array (size=3)
...
1 =>
array (size=6)
'id' => int 2
'category_id' => int 1
'questions' => string 'Ecology deals with' (length=18)
'answer_id' => int 10
'image_path' => string '' (length=0)
'options' =>
array (size=4)
0 =>
array (size=3)
...
1 =>
array (size=3)
...
2 =>
array (size=3)
...
3 =>
array (size=3)
...
but Actually I am not getting options key into my questions array
The issue here is when you do a foreach, you are referring to the array by value - that is, under the hood PHP will create a copy of the variable inside the array. What you need is to access each element in the foreach by reference (note the & next to $q)
foreach ($questions as & $q) {
$q['options'] = Option::where('question_id', '=', $q['id'])->get()->toArray();
}
See:
Passing by references and the PHP docs on foreach.
Note: This is not really a Laravel issue, just PHP.
Try this (& in foreach):
foreach ($questions as & $question) {
$question['options'] = Option::where('question_id', $question['id'])->get()->toArray();
}
You can try:
foreach ($questions as $key=>$question) {
$questions[$key]['options'] = Option::where('question_id',$question['id'])->get()->toArray();
}
how can i get all values from multidimensional associative array
I dont want to use print_r want to control my array put all the value in normal array with unique values
my array is look like this
array (size=10)
0 =>
array (size=3)
0 =>
array (size=1)
'Campaign' => string 'DEMO' (length=4)
1 =>
array (size=1)
'Campaign' => string 'Home_Sec' (length=8)
2 =>
array (size=1)
'Campaign' => string '' (length=0)
1 =>
array (size=0)
empty
2 =>
array (size=0)
empty
3 =>
array (size=1)
0 =>
array (size=1)
'Campaign' => string 'Back_Brace' (length=10)
4 =>
array (size=2)
0 =>
array (size=1)
'Campaign' => string 'Home_Sec' (length=8)
1 =>
array (size=1)
'Campaign' => string '' (length=0)
5 =>
array (size=1)
0 =>
array (size=1)
'Campaign' => string 'home_Sec_2' (length=10)
6 =>
array (size=1)
0 =>
array (size=1)
'Campaign' => string 'Burial_Ins' (length=10)
7 =>
array (size=0)
empty
8 =>
array (size=0)
empty
9 =>
array (size=0)
empty
I dont want to use print_r want to control my array put all the value in normal array with unique values
array_walk is an option, but here's another option if you want to try something a bit more coded by yourself, solving this problem recursively
This will flatten any n-max level array into a single array that contains all the values of all the sub arrays (including the initial array itself)
<?php
$array = array(
1 => array(1, 2, 3, 4 => array(
1, 2, 3, 4
)),
4, 5);
function recurse_values($array) {
if (is_array($array)) {
$output_array = array();
foreach ($array as $key=>$val) {
$primitive_output = recurse_values($val);
if (is_array($primitive_output)) {
$output_array = array_merge($output_array, $primitive_output);
}
else {
array_push($output_array, $primitive_output);
}
}
return $output_array;
}
else {
return $array;
}
}
print_r(recurse_values($array));
?>
If you need unique values, at the end you can add a array_unique to do this.
You can use array_walk
$array = array(...); //your values here
function output($item, $key) {
echo $key . ' =>' . $item;
}
array_walk($array, 'output');
Are you asking how you can "flatten" this multi-dimensional array into a one dimension? Possible solutions to similar problems... How to Flatten a Multidimensional Array?