This is my loop:
foreach ($array as $key => $value) {
$array1[$value->getUuid()][$value->getFields()->getName()] = $value->getContent();
}
The result is:
array:2 [▼
"d8ab80f4f6" => array:16 [▶]
9087785727 => array:16 [▶]
]
I do not understand, why the last key does not have quotes. How can I prevent this?
Related
My project has an array made of a request with an array of inputs inside, as it follows:
array:8 [▼
"type_id" => array:1 [▼
0 => "1"
]
"zip_code" => array:1 [▼
0 => "88801500"
]
"street_address" => array:1 [▼
0 => "Avenida Getúlio Vargas"
]
"number" => array:1 [▼
0 => "asdasd"
]
"street_address_2" => array:1 [▼
0 => "asdasd"
]
"city_id" => array:1 [▼
0 => "4384"
]
"neighborhood" => array:1 [▼
0 => "Centro"
]
"created_by" => 2
]
But when I try to run said array on a foreach to insert it on the database, I get a result that doesnt make sense:
array:1 [▼
0 => "1"
]
My code:
dd($dataEnderecos); //This is for debug purposes, it shows the initial array on this question.
foreach ($dataEnderecos as $enderecos) {
dd($enderecos); //This is for debug purposes, it shows the secondary array on this question.
$enderecoID = $this->address->create($enderecos)->id;
$this->repository->enderecos()->attach($enderecoID);
}
I managed to fix this by using a for loop and using another variable to receive the results of the initial array:
for ($i = 0; $i < $limit; $i++) {
$insert = array(
'type_id' => $dataEnderecos['type_id'][$i],
// ^^^^ ^^^^ ^^^^
// Initial Array Secondary Array Index
);
}
I create my arrays like this:
foreach ($array as $key => $value) {
$array1[$value->getUuid()][$value->getFields()->getName()] = $value->getContent();
}
The result is array1:
array:2 [▼
"d8ab80f4f6" => array:16 [▶]
9087785727 => array:16 [▶]
]
I create another array in a little bit different way, array2:
array:2 [▼
"d8ab80f4f6" => array:3 [▶]
9087785727 => array:3 [▶]
]
Now I want to merge those arrays:
$output = array_merge_recursive($array1,$array2);
The output is:
array:3 [▼
"d8ab80f4f6" => array:19 [▶]
0 => array:3 [▶]
1 => array:16 [▶]
]
But I expect the output to be:
array:3 [▼
"d8ab80f4f6" => array:19 [▶]
"9087785727" => array:19 [▶]
]
array_merge and array_merge_recursive treat string keys differently from numeric keys:
If the input arrays have the same string keys, then the values for these keys are merged together into an array, and this is done recursively, so that if one of the values is an array itself, the function will merge it with a corresponding entry in another array too. If, however, the arrays have the same numeric key, the later value will not overwrite the original value, but will be appended.
That's what's happening here. The key 9087785727 is numeric, so those entries are not being merged.
So you need to write your own loop.
$output = [];
foreach ($array1 as $key => $value) {
$output[$key] = array_merge($value, $array2[$key]);
}
DEMO
You can use next foreach loop with reference &:
foreach($ar1 as $key=>&$subar){
$subar = array_merge($subar,$ar2[$key]);
}
Demo
i have form to make a bill for multiple products,
that form return request like this
#parameters: array:5 [▼
"quantity" => array:2 [▼
0 => "1"
1 => "2"
]
"product" => array:2 [▼
0 => "Mr. Jasen Beer,OliveDrab,XS"
1 => "Carlotta Yundt"
]
"date" => array:2 [▼
0 => "2019-12-29"
1 => "2019-12-29"
]
"id" => array:2 [▼
0 => "15"
1 => "11"
]
]
}
i need to loop all arrays to make insert all at once
thanks.
Try to loop it like this, you will get the array wrap every product's attributes:
$inserted_array = [];
foreach($parameters as $key => $values) {
foreach($values as $index => $val) {
$inserted_array[$index][$key] = $val;
}
}
\DB::table('table_name')->insert($inserted_array);
And I found that there is id in your array, remember to make it fillable.
Working on a Laravel application whereby I am consuming some data from an API. I get the response as a JSON object and convert to array. It appears as a complex multi-dimensional array
(nested arrays). Am trying to loop through it using a nested foreach so as to reach out to the id of each item but I keep failing..
The response is stored in a variable called usmDet
The array response
array:1 [▼
0 => array:1 [▼
0 => array:3 [▼
"id" => "74696"
"agents" => array:13 [▶]
"policies" => array:481 [▶]
]
1 => array:3 [▼
"id" => "1525"
"agents" => array:8 [▶]
"policies" => array:357 [▶]
]
]
1 => array:1 [▼
0 => array:3 [▼
"id" => "73401"
"agents" => array:1 [ …1]
"policies" => array:8 [ …8]
]
1 => array:3 [▼
"id" => "210"
"agents" => array:13 [ …13]
"policies" => array:773 [ …773]
]
]
]
My nested foreach
foreach($usmDet as $key => $value){
if(is_array($value)){
foreach($value as $key => $value){
echo $key." ".$value."<br>";
}
}
echo "<br>";
}
The id is part of the array, as you can access it like $value['id']
In the second foreach to prevent confusion you should select a different name for the key and the value.
Try it like this:
foreach($usmDet as $key => $value){
if(is_array($value)){
foreach($value as $k => $v){
echo $v['id'] . "<br>";
}
}
}
Result:
74696
1525
73401
210
Php demo
To get all the values for key "id" when multiple nested arrays, you could use array_walk_recursive
$ids = [];
array_walk_recursive($usmDet, function($value, $key) use (&$ids){
if ($key === "id") {
$ids[] = $value;
}
});
print_r($ids);
Php demo
I have a following assoc array:
45111 => array:16 [▼
"სქესი" => array:1 [▶]
"ასაკი" => array:1 [▶]
"შემოსავალი" => array:1 [▶]
"შემოსავლის ტიპი" => array:1 [▶]
"Existing PMT" => array:1 [▶]
"min(payment cnt)" => array:1 [▶]
"guarantor" => array:1 [▶]
"reqeusts last 3m / req last 12m" => array:1 [▶]
"micro loans cnt last 12m (closed)" => array:1 [▶]
"micro loans cnt last 3m (closed)" => array:1 [▶]
"loans deliquency days" => array:1 [▶]
"Past PMT" => array:1 [▶]
"RiskGrade" => array:1 [▶]
"ProbabilityOfDefault" => array:1 [▶]
"Total" => array:1 [▶]
"penalties paid" => array:1 [▼
0 => 90.0
]
]
I'd like to iterate over the second array using first index. However, it returns "Undefined offset" exception. I guess $v[0] is not supported in blade. What is the correct way to do this?
#foreach ($creditInfo as $k => $v)
#foreach ($v[0] as $y => $x)
<tr>
<td>{{$y}}</td>
</tr>
#endforeach
#endforeach
It's just executing PHP so to say it's not supported in Blade makes no sense. The error happens because $v[0] does not exist as 0 is an undefined offset. $v['Total'] would be fine though and would return an array.
Since the first key is სქესი, 0 isn't going to work but if you are only concerned about the first item, you can use current to grab the first.
#foreach ($creditInfo as $k => $v)
#foreach (current($v) as $y => $x)
<tr>
<td>{{$y}}</td>
</tr>
#endforeach
#endforeach
That will loop through the array in $v['სქესი']