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);
Related
I'm a little confused, and I need help.
I have a foreach loop in my code below. I need to put the foreach loop into the $data = array('image' => $final_image);
How can i put foreach loop in array ? Anyone can help me please.
$getImages = isset($GetNewMessageU['imageid']) ? $GetNewMessageU['imageid'] : NULL;
$ExploreImage = explode(",", $getImages);
$CountExplodes=count($ExploreImage);
foreach($ExploreImage as $a) {
$newdata=$Post->Post_GetUploadChatImageID($a);
if($newdata){
$final_image=$base_url."uploads/images/".$newdata['uploaded_image'];
}
echo $final_image;
}
if($GetNewMessageU){
$json = array();
$data = array(
'image' => $final_image,
);
$result = json_encode( $data );
echo preg_replace('/,\s*"[^"]+":null|"[^"]+":null,?/', '', $result);
}
$getImages = isset($GetNewMessageU['imageid']) ? $GetNewMessageU['imageid'] : NULL;
$ExploreImage = explode(",", $getImages);
$CountExplodes=count($ExploreImage);
// Create an array to store final images
$final_images = [];
foreach($ExploreImage as $a) {
$newdata=$Post->Post_GetUploadChatImageID($a);
if($newdata){
$final_image=$base_url."uploads/images/".$newdata['uploaded_image'];
// Save the image if its new data.
$final_images[]= $final_image;
}
}
if($GetNewMessageU){
$json = array();
$data = array(
'image' => $final_images, // We pass the final images array
);
$result = json_encode( $data );
echo preg_replace('/,\s*"[^"]+":null|"[^"]+":null,?/', '', $result);
}
Made things a bit more verbose.
First define the array.
Then in the if statement add element to the array.
$getImages = isset($GetNewMessageU['imageid']) ? $GetNewMessageU['imageid'] : NULL;
$ExploreImage = explode(",", $getImages);
$CountExplodes=count($ExploreImage);
$final_images = array(); // Define object as array
foreach($ExploreImage as $a) {
$newdata=$Post->Post_GetUploadChatImageID($a);
if($newdata){
$final_image=$base_url."uploads/images/".$newdata['uploaded_image'];
$final_images[] = array('image' => $final_image); // Will create new array item
}
echo $final_image;
}
if($GetNewMessageU){
$json = array();
$data = array(
'image' => $final_image,
);
$result = json_encode( $data );
echo preg_replace('/,\s*"[^"]+":null|"[^"]+":null,?/', '', $result);
}
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 am using this code to list all files inside a directory which works perfectly
<?php
$exclude = array("index.php","cssheadertop.php","cssheaderbottom.php");
$cssfiles = array_diff(glob("*.php"), $exclude);
foreach ($cssfiles as $cssfile) {
$filename = "http://example.com/lessons/css/".$cssfiles[$cssfile];
outputtags($filename,true,true);
}
?>
However, with this code nothing is shown on the webpage. I can't figure out why
<?php
$exclude = array("index.php","htmlheadertop.php","htmlheaderbottom.php");
$htmlfiles = array_diff(glob("*.php"), $exclude);
foreach ($htmlfiles as $htmlfile) {
$filename = "http://example.com/lessons/html/".$htmlfiles[$htmlfile];
outputtags($filename,true,true);
}
?>
Try this:
<?php
$exclude = array("index.php","htmlheadertop.php","htmlheaderbottom.php");
$htmlfiles = array_diff(glob("*.php"), $exclude);
foreach ($htmlfiles as $htmlfile) {
$filename = "http://example.com/lessons/html/".$htmlfile;
outputtags($filename,true,true);
}
?>
$htmlfiles[$htmlfile] should not be set and should not work.
You need to use $htmlfile inside foreach loop instead of $htmlfiles[$htmlfile], and it works with any other variable's name
<?php
$exclude = array("index.php","htmlheadertop.php","htmlheaderbottom.php");
$htmlfiles = array_diff(glob("*.php"), $exclude);
foreach ($htmlfiles as $htmlfile) {
$filename = "http://example.com/lessons/html/".$htmlfile;
outputtags($filename,true,true);
}
?>
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);