I have a foreach like :
foreach($values as $key => $value){
$name = $value['name'];
if(!isset($array[$value['position']])){
$array[$value['position']] = new \stdClass();
}
$array[$value['position']]->$name = $value['value'];
}
So in my loop, in $value, I have $name, $position and $value.
$name is the name of the property
$value is the value
$position is the position of the object in the array (index)
In this loop, i check first in my object is already created in the specific index (position), if not i create it, and after i put the value in the correct property.
My problem is, if in my array of value i don't have all the positions following (0,1,2,3) but for example (0,1,3,4), my script bug and i don't have the correct array at the end.
How can i detect if i miss an index ? And how can i fix this ?
Thanks for help !
PS : I don't know if you have enough informations.. tell me !
EDIT :
At the end, the correct output must be :
[Object { name="d", complet="false"}, Object { adresse="d", complet="false"}]
But, if the script bug with the index, i have :
Object { 0={...}, 1={...}, 2={...}, plus...}
Related
I need to get values of array through a loop with dynamic vars.
I can't understand why the "echo" doesn’t display any result for "$TAB['b']".
Do you know why ?
The test with error message : https://3v4l.org/Fp3GT
$TAB_a = "aaaaa";
$TAB['b'] = "bbbbb";
$TAB['b']['c'] = "ccccc";
$TAB_paths = ["_a", "['b']", "['b']['c']"];
foreach ($TAB_paths as $key => $value) {
echo "\n\n\${'TAB'.$value} : "; print_r(${'TAB'.$value});
}
You are treating the array access characters as if they are part of the variable name. They are not.
So if you have an array $TAB = array('b' => 'something');, the variable name is $TAB. When you do ${'TAB'.$value}, you're looking for a variable that's actually named $TAB['b'], which you don't have.
Since you say that you just want to be able to access array indexes dynamically based on the values in another array, you just put the indexes alone (without the array access characters) in the other array.
$TAB['b'] = 'bbbbbb';
$TAB['c'] = 'cccccc';
$TAB_paths = array('b', 'c');
foreach ($TAB_paths as $key => $value) {
echo "\n\n".'$TAB['."'$value'".'] : ' . $TAB[$value];
}
Output:
$TAB['b'] : bbbbbb
$TAB['c'] : cccccc
DEMO
It's unclear what you're trying to do, although you need to include $TAB_all in $TAB_paths:
$TAB_paths = [$TAB_all['a'], $TAB_all['aside']['nav']];
Result:
${TAB_all.aaaaa} : ${TAB_all.bbbbb} :
Not certain what you're needing. My guess you need to merge two arrays into one. Easiest solution is to use the array_merge function.
$TAB_paths = array_merge($TAB_a1, $TAB_a2);
You can define the variable first
foreach ($TAB_all as $key => $value) {
${"TAB_all" . $key} = $value;
}
Now Explore the result:
foreach ($TAB_all as $key => $value) {
print_r(${"TAB_all" . $key});
}
I have an array composed of files of a folder.
When I use the following snippet :
foreach($myarray as $key => $value)
{
echo $value. "<br>";
}
I have the following output :
vendor/templates/File1.docx
vendor/templates/File2.docx
vendor/templates/File3.docx
My question is : how to make it to put each value of my array in a new variable ? How to make it automatically if I have e.g 100 files in my folder ?
Actually I'd like to have (if my array is only composed of 3 items) :
$a = 'vendor/templates/File1.docx'
$b = 'vendor/templates/File2.docx'
$c = 'vendor/templates/File3.docx'
I guess I should use a loop but after many tests, I'm still getting stuck..
Have you any ideas ?
Thanks !!
Here is the solution:
<?php
$myarray = ['vendor/templates/File1.docx', 'vendor/templates/File2.docx', 'vendor/templates/File3.docx'];
foreach($myarray as $key => $value)
{
$varname = "var".$key;
$$varname = $value;
}
echo $var0."\n";
echo $var1."\n";
echo $var2."\n";
->
vendor/templates/File1.docx
vendor/templates/File2.docx
vendor/templates/File3.docx
May be following function will work for your problem.
extract($array);
http://php.net/extract
My problem is, i have a 2D Array and i want to override the value of an element that was set.
My code is as follows:
$indes="-1";
$inupccode="-1";
$inuomcode="-1";
$instdpack="-1";
$inweight="-1";
$inlength="-1";
$inwidth="-1";
$inheight="-1";
$inunitprice="-1";
$infamcode="-1";
$basicInfo = array(array($indes,"prdes1_35", $inupccode,
"prupc#_2", $inuomcode,"prwuts_3-1",
$instdpack,"prwuns_12", $inweight,
"prgrwt_11", $inlength,"prlong_7",
$inwidth,"prwide_7", $inheight,"prhigh_7",
$inunitprice,"prprce_12", $infamcode,"proga2"));
echo "before";
print_r($basicInfo);
foreach($basicInfo as $value){
foreach ($value as $id){
if($id == "prdes1_35"){
$value = 2;
}
}
}
echo "after";
print_r($basicInfo);
In the code for the value of $indes i would like to change from -1 to 2 and the reminder array value must remain as "-1". How can i achieve this?
Biggest problem first: I dont think your code does what you intent to achieve. Are you sure you understand (multi dimensional) arrays?
The other issue is the way you try to change $value. Please check this post:
(https://stackoverflow.com/questions/15024616/php-foreach-change-original-array-values)
for information on how to change an array you run through with foreach.
Simplest solution for you would be:
foreach($basicInfo as &$value){
foreach ($value as $id){
if($id == "prdes1_35"){
$value = 2;
}
}
}
Mind the ampersand (&) before the $value variable. The referenced post an the link in there hold all the explanation necessary to understand what it means.
Perfomancewise this would be the best solution, especially when your array gets big:
foreach($basicInfo as $key => $value){
foreach ($value as $id){
if($id == "prdes1_35"){
$basicInfo[$key][$value] = 2;
}
}
}
NOTE: I did not change your codes logic (that is broken, I assume). I just demonstrated how to change a value when iterating through an array with foreach.
I have the following array. What I'm trying to do is get each element under "bill_ids", use the ID (e.g. "hjres61-114") to make another call and then retitle the 0 under "bill_ids" to the ID and then include another array under that element.
Here's what I have and it's giving me this error..
Message: Illegal offset type
$floor_updates = $this->congress->floor_updates($params);
foreach ($floor_updates as $update) {
if ($update['bill_ids']) {
foreach ($update['bill_ids'] as $bill => $bill_id) {
$billInfo = $this->bill->billSearch(['bill_id' => $bill_id]);
$floor_updates[$update]['bill_ids'][$bill][0] = $billInfo;
}
}
}
I'm terrible with php arrays and any guidance would be greatly appreciated..
What you actually want to do is the following:
First, capture the array index of each of our update elements. We can simply do that by passing in $array_index => $update.
foreach ($floor_updates as $array_index => $update)
Now, we can access the $update array by $floor_updates[$array_index].
$floor_updates[$array_index]['bill_ids'][$bill] = $billInfo;
In the above, there's no reason to access the 0th element of the array as the $bill actaully contains reference to the index of each key value pair, so we can simply just reference [$bill] to get access to the array.
I know this should be very simple, but boy I'm making a mess of it... would be great if someone could point me in the right direction.
I've got an array which looks like this:
print_r($request_attributes['length']);
Array
(
[0] => 28.00000
[1] => 18.00000
)
and am trying to modify like so:
if(is_array($request_attributes['length'])) {
$request_attributes['length'] = $request_attributes['length'][0];
print($request_attributes['length']);
$request_attributes['length'] = $request_attributes['length'][1];
print($request_attributes['length']);
}
which gives the correct output in the first update, but the second item outputs an '8'. I've tried the above in both a for and foreach which results in similar output for both this and the other two arrays ( width(8) and height(0) - they should result in 18.00000 and 13.00000 respectively ). So I guess I really have two questions:
1. How do I update this(these) element(s)?
2. Where are the funny outputs actually coming from?
If anyone can help, I'd really appreciated it.
Just have a look at this. Your problem is, that you override you variable and in the second step $request_attributes['length'] is a string. Just define another var for your values.
$request_attributes['length'] = [
28.000,
18.000
];
$attributes = array();
if (is_array($request_attributes['length'])) {
foreach ($request_attributes['length'] as $value) {
$attributes[] = $value;
}
}
As you see $attributes will contain all values of your $request_attributes['length'] array and will not be overwritten.
Define araay as below
$val=array([0]=>"18.000",[1]=>13.000)
then use
if(is_array($request_attributes['length'])) {
$request_attributes['length'] = $val;
print_r($request_attributes['length']);
$request_attributes['length'] = $val;
print_r($request_attributes['length']);
}
Previously your array doesnt have any name.
Your print will only return Just array not the values
use
print_r($request_attributes['length']) ;
instead