This is probably quite simple to do.. but I just can't think of how to do this.
I have a photo upload script, I want to return two types of data in an array, right now I just have one set of data for the photo returned.
right now I do this:
$photos = array();
///Script for photo upload etc.
array_push($photos, $newPhotoToAdd)
//then when it's finished uploading each photo i do json_encode:
print json_encode($photos);
all of this works perfect, however now I want to return another set of data with each photo.
I need to do something like:
array_push($photos, ['photoID'] = $photoID, ['photoSource'] = $newPhotoToAdd)
Original Question
You can just push an array on instead of newPhotoToAdd like the following:
$photos = array();
// perform upload
$photos[] = array(
'photoID' => $photoID,
'photoSource' => $newPhotoToAdd
);
print json_encode($photos);
You'll notice that I have swapped array_push() DOCs for the array append syntax of [] DOCs as it is simpler and with less overhead:
Note: If you use array_push() to add one element to the array it's
better to use $array[] = because in that way there is no overhead of
calling a function.
From comments: printing it out in JavaScript
You are dealing with an array of objects so you need to loop over them:
for(var i in json) {
alert(json[i].photoID);
}
You want to create an array like this:
$photo = array('photoID' => $photoID, 'photoSource' => $newPhotoToAdd);
Then you want to do the push as you did before with this new array:
array_push($photos, $photo);
do something like:
$photos = array();
array_push($photos, array('photoID'=>$photoID,'photoSource'=>$newPhotoToAdd));
foreach ($photos as $photo) {
print json_encode($photo);
}
Related
I want to access to 'image'
I reached my array 'questions' with this $file = $request->files->get('quiz')['questions'];
but I can't go further
Can you help me please
Based on your screen shot you could,
$image = $request->files->get('quiz')['questions'][0]['image'];
Each nested level is an array so you can walk all the way down using the array keys.
Note that [0] will be the first question, but you probably want all the questions. If so, you can loop over them.
foreach ($request->files->get('quiz')['questions'] as $question) {
$image = $question['image'];
//
}
The scalaire value on the get is depreciated now.
You can use the "all" :
foreach ($request->files->all('quiz')['questions'] as $question) {
$image = $question['image'];
}
There is this library https://github.com/halaxa/json-machine (PHP) that allows me to read huge JSON files without loading the entire file to memory. It works great and the way I read the JSON is like this:
$temp = \JsonMachine\JsonMachine::fromFile("teste.json");
foreach ($temp as $key => $value) {
echo $value;
}
The problem is that I cant use foreach, I need to only read the JSON as I need. For example, I tried the code below everytime I need to retrieve an element from the array:
echo next($temp);
However it returns and empty string. If I use var_dump(current($temp)) it returns this:
object(JsonMachine\StreamBytes)#2 (1) { ["stream":"JsonMachine\StreamBytes":private]=> resource(10) of type (stream) }
Using the foreach loop works perfectly, but I cant use it, I need to retrieve the elements as I need. How can I do that?
This class already provides a generator, you should be able to do something like this:
$temp = \JsonMachine\JsonMachine::fromFile("teste.json");
$iterator = $temp->getIterator();
$firstItem = $iterator->current();
$iterator->next();
$secondItem = $iterator->current();
$iterator->next();
$thirdItem = $iterator->current();
[Edit] Looks like JsonMachine::getIterator() returns a chained generator, so just change that second line to this:
$iterator = $temp->getIterator()->getIterator();
I have this
$homedata = $user->getbanks();
foreach ($homedata as $data)
echo json_encode(array("replies" => array(array("message" => $data->bankname))));
I have this put the result am getting is only one array from the list. I want to get all.
If I understand your requirement correctly, you need the banknames in an array replies within a key message. For that the below code will do the job. But you can optimize the query for better performance.
$homedata = $user->getbanks();
$banks['replies'] = [];
foreach ($homedata as $data) {
$banks['replies'][]['message'] = $data->bankname;
}
print_r(json_encode($banks)); exit;
Further, you were only getting the first content because you are overriding the previous content when you iterate using foreach.
Basically I am calling an API to get an array of URLs for an image.
I start off by doing this
$mainResponse = array(
"result" => array(
),
"ack" => "success"
);
I will then make my call and add the image URLs like this:
foreach($resp->Item as $item) {
$picture = $item->PictureURL;
array_push($mainResponse['result'], $picture);
}
Finally, I will echo this out to me.
echo json_encode($mainResponse);
The problem I am facing is that my response is
{"result":[{"0":"IMAGE_URL","1":"IMAGE_URL"}],"ack":"success"}
Where I would want it to be like....
{"result":["IMAGE_URL","IMAGE_URL"],"ack":"success"}
Where did I go wrong in my PHP code?
Seems like $picture is a associative array, change the foreach loop with this:
foreach($resp->Item->PictureURL as $item) {
foreach($item as $_item){
array_push($mainResponse['result'],$_item);}
}
For some reason this API returns an object instead of an array.
You can just do:
foreach ($resp->Item as $item) {
$picture = $item->PictureURL;
array_merge($mainResponse['result'], (array)$picture);
}
You can use array_push if you want every item to have separate pictures.
I have always sucked at complex arrays there must be something in my brain preventing me from ever understanding them. I will try to make this example really simple so we will not go off topic. I use this code to use numbers to represent each file name:
$mod_nums = array('1' => $input_zip_path . '01_mod_1.0.2.zip',
'2' => $input_zip_path . '02_mod_1.0.1.zip',
);
So when I use $mod_nums['01'] it will display the path to that file. I have an array from the script that put these $mod_nums values into an array like so:
$files_to_zip = array(
$mod_nums['1'],
$mod_nums['2']
);
That worked fine. Now I wanted to add a $_POST value so that I can enter numbers like 1,2 and other numbers that I add to the $mod_nums array later like 1,3,6,12 for example. So I used an explode for those posted values:
$explode_mods = explode(",", trim($_POST['mods']));
Now for the big question that is racking my brain and spent hours on and cannot get it to work.... I need for $files_to_zip to still be in an array and display the posted values of $mod_nums. So it would be like:
$files_to_zip = array( HAVE $_POSTED VALUES IN HERE );
I hope that makes sense. I need $files_to_zip to remain in array format, grab the file path to the zip files from the $mod_nums array, and display it all correctly so it would dynamically output:
$files_to_zip = array('01_mod_1.0.2.zip', '02_mod_1.0.1.zip');
so posted numbers will appear in an array format for the $files_to_zip variable. Make sense? In short I need an array to have dynamic values. Thanks :)
EDIT
Phew I figured it out myself from memory when I worked on something similar many years ago. This looks tough but it isn't. I had to use a foreach and assign the variable into an array like so:
$blah = array();
foreach ($explode_mods as $value)
{
$blah[] = $mod_nums[$value];
}
then I just assigned $files_to_zip to $blah:
$files_to_zip = $blah;
works perfectly :) I just forgot how to dynamically assign values into an array.
// filenames array
$mod_nums = array('1' => $input_zip_path . '01_mod_1.0.2.zip',
'2' => $input_zip_path . '02_mod_1.0.1.zip',
);
// mod_num keys
$explode_mods = explode(',', trim($_POST['mods']));
// array to hold filenames
$files_to_zip = array();
// loop over all the mod_num keys submitted via POST
foreach($explode_mods as $key){
// save the filename to the corresponding array
$files_to_zip[] = $mod_nums[$key];
}
maybe i havn't understood you right, but won't this just be a simple foreach-loop to add the entrys to $files_to_zip like this:
$explode_mods = explode(",", trim($_POST['mods']));
foreach($explode_mods as $k){
$files_to_zip[] = $mod_nums[$k];
}