I know I can add elemnts to an array like this:
$arr = array("foo" => "bar", 12 => true);
Now, how can I do that in a foreach using dynamic values? I have this code:
foreach ($values as $value) {
$imagePath = $value->getImagePath();
$dependsOn = $value->getDependsOn();
$dependsOn = explode(':', $dependsOn);
$dependsOnOptionValueTitle = trim($dependsOn[1]);
array_push($paths, $dependsOnOptionValueTitle => $imagePath); // not working
}
How can I add key/value pairs to my $paths array?
Instead of
array_push($paths, $dependsOnOptionValueTitle => $imagePath); // not working
you should be able to use
$paths[$dependsOnOptionValueTitle] = $imagePath;
From what I can see, this is what you're trying to do:
$paths[$dependsOnOptionValueTitle] = $imagePath;
Comment if I'm wrong and I'll try to fix it.
Related
Look at my code below, first, I want to combine three arrays together to create content in the foreach loop, and then create the final array by using the first array and the content string inside the foreach. It is kind of challenge for me, help, appreciate.
<?php
//above code, I deleted them, unnecessary to show
$fruit = explode(',',$fruit);
$type = explode(',',$type);
$date = explode(',',$date);
foreach (array_combine($fruit, $type, $date) as $fruit => $type => $date) {
echo $content = $fruit.'is'.$type.'at'.$date;
}
//create my final array
$total = array(
'date'=>$date,
'content'=>$content
);
?>
Please try:
<?php
$fruits = explode(',',$fruit);
$types = explode(',',$type);
$dates = explode(',',$date);
$total=array();
foreach ($fruits as $index=>$fruit) {
$type=$types[$index];
$date=$dates[$index];
echo $content = $fruit.'is'.$type.'at'.$date;
$total[]=array('fruit'=>$fruit,'date'=>$date,'type'=>$type,'content'=>$content);
}
?>
I am generating an array using a foreach like so...
<?php
$docs = array();
$media = get_attached_media('image');
foreach($media as $medias) {
$docs[] = $medias->guid;
}
$images = serialize(array('docs' => $docs));
print_r($images);
?>
The output I am getting is...
a:1:{s:4:docs";a:3:{i:0;s:62:"http://www.example.com/image1.jpg";i:1;s:62:"http://www.example.com/image2.jpg";i:2;s:62:"http://www.example.com/image3.jpg";}}"
But what I need is...
a:1:{s:4:"docs";a:4:{i:0;a:1:{s:15:"property_imgurl";s:35:"http://wwww.example.com/image1.jpg";}i:1;a:1:{s:15:"property_imgurl";s:35:"http://wwww.example.com/image2.jpg";}i:2;a:1:{s:15:"property_imgurl";s:35:"http://wwww.example.com/image3.jpg";}i:3;a:1:{s:15:"property_imgurl";s:35:"http://wwww.example.com/image4.jpg";}}}
Where am I going wrong?
It looks like your expecting $medias->guid to be an array, but it's a string. I believe your going to need to provide an array value when pushing into your array. This should work for you:
$docs = array();
$media = get_attached_media('image');
foreach($media as $medias) {
$docs[] = array("property_imgurl" => $medias->guid);
}
$images = serialize(array('docs' => $docs));
print_r($images);
I want to replace an array code inside my php file with new array dynamically
For e.g
<?php
$my_Array = array("key1" => "valu1");
?>
to
<?php
$my_Array = array("key2" => "valu2");
?>
Is that possible?
You can do this using url parameters like below.
Your url should be like mywebsite.com/test.php?key1=value1&key2=value2
In your php file
$myarray = array();
foreach ($_GET as $key => $value) {
$myarray[$key] = $value;
}
print_r($myarray);
Output should be like below
Array ( [key1] => value1 [key2] => value2 )
If it's saved in file and you need to replace it, use something like this:
<?php
$file = 'filename.php'; // path to the file
$data = file_get_contents($file);
$old = array("key1","valu1"); // old values to replace
$new = array("key2","valu2"); // the new values
$data = str_replace($old,$new,$data);
file_put_contents($file,$data);
?>
Is this what you need?
I have this loop, where I'm trying to generate an array like this:
array( $file['file_type'] => $file['import_status] )
To do so, I build this foreach loop to access the $file variable, which has the data I wanted to store.
$filesStatusArray = array();
foreach ($filesToImport as $keys => $file) {
$filesStatusArray = $file['import_status'];
}
This way I'm doing, I have only an array with the import_status values.. but I wanted the key to be the file_type... how can I do that?
$filesStatusArray = array();
foreach ($filesToImport as $keys => $file) {
$filesStatusArray[$file['file_type']] = $file['import_status'];
}
I have a function that I need to pass 4 distinct variables to, and these variables values come from an array:
$pagesArray = array(
'pre-file1.html' => 'blahblah1',
'post-file1.html' => 'blahblah2'
);
$file1 = 'blahblah1';
$file2 = 'blahblah2';
$file1Name = 'pre-file1.html';
$file2Name = 'post-file1.html';
How do I assign in a foreach loop when I'm calling the function in the loop too?
I've tried this
foreach ($pagesArray as $fileName => $url)
{
$file1 = file($url);
$file2 = file($url);
$file1Name = $key;
$file2Name = $key;
compareFiles($file1, $file2, $file1Name, $file2Name);
}
But that doesn't work because it's calling the function in the loop and will only loop over after it's called every time.
NB: the above is only an example, there will be more objects in that array than the two currently shown.
list($file1, $file2) = array_values($pagesArray);
list($file1Name, $file2Name) = array_keys($pagesArray);
compareFiles($file1, $file2, $file1Name, $file2Name);