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);
Related
I need to get a JSON with arrays, how I could do it?
//JSON I need to get
{"keywords":[{"keyword":"kw1", "tags":["sample"]},{"keyword":"kw2", "tags":["sample, sample2"]}]}
//For now, I got this
$keywords = array("kw1", "kw2");
$tags= array("sample", "sample2");
function Keywords($keywords, $tags){
$fields= array("keywords" => $keywords);
$jsondata = json_encode($fields);
print_r($jsondata );
}
//output
{"keywords":["kw1","kw2"]}
I expect the output like this:
{"keywords":[{"keyword":"kw1", "tags":["sample"]},{"keyword":"kw2", "tags":["sample, sample2"]}]}
Assuming tags in the first element of your example is supposed to be ["sample, sample2"] as well (otherwise you would really have to explain by what logic you want to achieve at the result as shown) …
$keywords = array("kw1", "kw2");
$tags= array("sample", "sample2");
$result = new StdClass;
$result->keywords = [];
foreach($keywords as $keyword) {
$temp = new StdClass;
$temp->keyword = $keyword;
$temp->tags = [];
foreach($tags as $tag) {
$temp->tags[] = $tag;
}
$result->keywords[] = $temp;
}
echo json_encode($result);
Basically two nested loops over the keywords and the tags, and inside a new temporary object is created an then appended to the result array.
I have a array of 3 images
$images
for ($i=0;$i<count($image); $i++) {
array_push($imgArray, $image[i]);
$valString = implode(',', $imgArray);
$setting = $valString;
print_r(settings);
}
o/p: img1.jpg,img2.jpg,img3.jpg
But I wnat the o/p as
{'ad1':img1.jpg,'ad2':img2.jpg,'ad3':img3.jpg}
i.e like a json.
Can anyoe please suggest help.Thanks.
if your array is ['img1.jpg','img2.jpg','img3.jpg'] then you should use below code
<?php
$images = ['img1.jpg','img2.jpg','img3.jpg'];
foreach($images as $key=>$image){
$images['ad'.($key+1)] = $images[$key];
unset($images[$key]);
}
print_r(json_encode($images));
?>
live demo : https://eval.in/823702
$arr = [];
for ($i=0;$i<count($image); $i++) {
$arr['ad'.$i+1] = $image[$i];
}
$settings = json_encode($arr);
print $settings;
I'm trying to use a variable in a foreach loop (with simplexml) but am confused as to how to get it work properly.
My variable is:
$path="channel->item";
And I want my foreach to look like:
foreach ($xml->".$path." as $newsItem)
But that doesn't work - like the $path is being echo as $path rather than it's contents.
if I do:
foreach ($xml->channel->item as $newsItem)
It works fine.
I'm sure it's just a syntax issue but I can't figure it out. Thanks in advance.
You can't include pointers in the variable variable.
$members = explode('->', $path);
foreach($xml->{$members[0]}->{$members[1]} as $v) {
}
This will only work if your path stays two dimensional.
[edit]
If you do need it to work recursively you can use this. Sorry took me a minute to write it:
function pathFinder($path, $obj) {
$members = explode('->', $path);
if(is_object($obj) && count($members) && property_exists($obj, $members[0])) {
$nextMember = $members[0];
array_shift($members);
return pathFinder(implode('->', $members), $obj->{$nextMember});
} else {
return $obj;
}
}
$xml = new stdClass;
$xml->channel->item[] = 'love';
$xml->channel->item[] = 'test';
$path = 'channel->item';
$array = pathFinder($path, $xml);
print_r($array);
output:
Array(
[0] => love
[1] => test
)
Try something like this:
#Object for test
$xml = new stdClass();
$xml->channel->item[] = "banana";
$xml->channel->item[] = "abacate";
$xml->channel->item[] = "manga";
$xml->channel->item[] = "abacaxi";
$xml->channel->item[] = "morango";
list($channel, $item)= explode('->','channel->item');
foreach ($xml->{$channel}->{$item} as $newsItem) :
var_dump($newsItem);
endforeach;
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.
i have this code
$dir = "img/ori/";
$dir_thumbs = "img/thumbs/";
$images = array();
$d = dir($dir);
while($name = $d->read()){
$thumb = "thumb_".$name;
$images[] = array('name' => $name,'thumb_url' => $dir_thumbs.$thumb);
}
$d->close();
$o = array('images'=>$images);
i want to output $name and $thumb_url of all the images. how to do it.? help me
<?php
foreach ($images as $image) {
echo "{$image['name']} {$image['thumb_url']}";
}
?>
Use print_r or var_dump for this:
print_r($o);
//or
var_dump($o);