I make an ajax request and the response its an array of images, the problem is that i echo the value of the array and its the one that i want, but when the foreach ends and i see what have the array every value its changed by the last item of the array
foreach ($x as $y) {
$auxImg->misc_id = $y->misc_id;
$auxImg->image = $y->image;
$aux[$i] = $auxImg;
echo $aux[$i]->image.' ';
//response of the array in the echo
/* 5/maqueta.png - 5/ponto.png - 5/ciades.jpg - 5/35235.jpg */
$i++;
}
echo var_dump($aux);
//response in the var_dump of the aux array
array(4) {
[0]=>
object(stdClass)#400 (2) {
["misc_id"]=>
int(9)
["image"]=>
string(11) "5/35235.jpg"
}
[1]=>
object(stdClass)#400 (2) {
["misc_id"]=>
int(9)
["image"]=>
string(11) "5/35235.jpg"
}
[2]=>
object(stdClass)#400 (2) {
["misc_id"]=>
int(9)
["image"]=>
string(11) "5/35235.jpg"
}
[3]=>
object(stdClass)#400 (2) {
["misc_id"]=>
int(9)
["image"]=>
string(11) "5/35235.jpg"
}
}
i really cant understand why this happend, that is the only time i use $aux var please help
The problem here is that $auxImg is all the time the same object, so in each step you modify this object and append it to array but because $auxImg is an object it's not copied.
You should add
$auxImg = new stdClass();
or
$auxImg = clone $auxImg;
(depending what code you use before loop)
after:
foreach ($x as $y) {
to get expected result.
Related
How can i get the first value of this array?
I want get jpeg value from each array, but it just returns "hh".
This is my code:
$image_output = wp_get_attachment_image_src( $id );
var_dump($image_output);
foreach ($image_output as $value) {
echo $value;
}
This is my array
array(4) {
[0]=>
string(98) "localhost/wp-content/uploads/2018/06/11.jpeg"
[1]=>
int(170)
[2]=>
int(120)
[3]=>
bool(true)
}
array(4) {
[0]=>
string(63) "localhost/wp-content/uploads/2018/06/10.jpeg"
[1]=>
int(170)
[2]=>
int(120)
[3]=>
bool(true)
}
Where is my code wrong?
If I understand it correct it's a multidimensional array with the link in item 0 of each array.
Echo implode("<br>\n", array_column($image_output, 0));
This should output the links one on each line without looping
I have a variable list of arrays, the list depend of user input. each time the program is called a new set of single arrays is generated.
I been searching for solutions but apparently all solution refer to array of arrays.
Originally the data are from database using:
for ($i = 0; $runrwos= mysql_fetch_assoc($run); $i++) {}
after the loop data are inserted in an array:
$data[$i] = array();
Using a foreach () {} function data are manipulated with mathematical operations to get the desired outcome.
The result will output two string of data,
$A is a numeric data
$B has alphabetic value
$explodA = explode(" ", $A);
$explodB = explode(" ", $B);
Then I combine the result
$new = array_combine($explodA, $explodB);
when I check result I with
var_dump($new);
I get the following result:
array(1) { [1204]=> string(8) "Home2017" }
array(1) { [1183]=> string(8) "Home2018" }
array(1) { [1204]=> string(4) "Stan" }
array(1) { [1204]=> string(7) "Jun2017" }
array(1) { [1173]=> string(9) "APRIL2017" }
.......................................
array(1) { [953]=> string(7) "UNE2018" }
array(1) { [1171]=> string(6) "MAY201" }
I need to sort this data as follow:
array(1) { [953]=> string(7) "UNE2018" }
array(1) { [1171]=> string(6) "MAY201" }
array(1) { [1173]=> string(9) "APRIL2017" }
array(1) { [1183]=> string(8) "Home2018" }
array(1) { [1204]=> string(8) "Home2017" }
array(1) { [1204]=> string(4) "Stan" }
array(1) { [1204]=> string(7) "Jun2017" }
Can someone direct in the right direction?
I guess the solution is simple but I cannot resolve
I return this array of objects from an API call like so. Note that $result is an array of arrays with $result[data] holding todo list objects and result[success] holding status of API call:
array(9) { [0]=> object(stdClass)#3 (5) { ["todo_id"]=> string(10) "1480430478" ["title"]=> string(13) "Check Printer" ["description"]=> string(8)
"Room 233" ["due_date"]=> string(10) "11/29/2016" ["is_done"]=> string(4) "true" } [1]=> object(stdClass)#4 (5) { ["todo_id"]=> string(10) "148043046" ["title"]=> string(18) "Doctor Appointment" ["description"]=> string(7)
"#4pm. " ["due_date"]=> string(10) "11/30/2016" ["is_done"]=> string(4) "true" }
etc..
I sort the array with usort fine and then I want to resort on the "is_done" field and put them at bottom of todo list in date order. The php to do this is :
//Sort by is_done
foreach($result[data] as $arrayElement ) {
foreach($arrayElement as $valueKey => $value) {
if(($valueKey == 'is_done') && ($value == 'true')){
$temp = $arrayElement;
//delete this particular object from the $array
unset($result[data][$arrayElement]);
array_push($result[data], $temp);
}
}
}
The problem I am having is my completed items are now at the end of the array but they are also still in their original position. The unset is not working. I have tried all variations on referencing the $result[data] item to no avail. This is probably something simple but I need some help if possible. Googling and checking this site shows no examples of unset in this type of situation. Thanks in advance.
Update:
after applying colburton's solution the API is now returning this data structure:
object(stdClass)#3 (6) { ["2"]=> object(stdClass)#4 (5) { ["todo_id"]=> int(1480698596) ["title"]=> string(7) "Test #4" ["description"]=> string(4) "test" ["due_date"]=> string(10) "12/02/2016" ["is_done"]=> string(5) "false" } ["3"]=> object(stdClass)#5 (5) { ["todo_id"]=> string(10) "1480617975" ["title"]=> string(13) "Check Printer" ["description"]=> string(4)
"Test" ["due_date"]=> string(10) "12/06/2016" ["is_done"]=> string(5) "false" } ["5"]=> object(stdClass)#6 (5) { ["todo_id"]=> int(1481136023) ["title"]=> string(9) "Todo item" ["description"]=> string(7) "test123" ["due_date"]=> string(10) "01/19/2017" ["is_done"]=> string(5) "false" } etc...
At the end of the call i do a
//json_decode the result
$result = #json_decode($result);
//check if we're able to json_decode the result correctly
if ($result == false || isset($result->success) == false) {
throw new Exception('Request was not correct');
}
//if there was an error in the request, throw an exception
if ($result->success == false) {
throw new Exception($result['errormsg']);
}
//if everything went great, return the data
return $result->data;
}
and then in main program I reference $result as
$result = $todo_items[0];
And that is where fatal error occurs now.
Update II:
Wanted to add that you then need to reindex the array
$result['data'] = array_values($result['data']);
I read here that this is a bug in json_decode. Thanks for the help....
Please use quotes around your array indices. This unsets what you want:
foreach ($result['data'] as $idx => $arrayElement) {
foreach ($arrayElement as $valueKey => $value) {
if (($valueKey == 'is_done') && ($value == 'true')) {
$temp = $arrayElement;
//delete this particular object from the $array
array_push($result['data'], $temp);
unset($result['data'][$idx]);
}
}
}
Seems really easy, but I can't seem to figure it out...
I have a simple line that gets mysql results through wordpress like this:
$sql_results = $wpdb->get_results($sql_phrase);
Then I parse it as JSON and echo it: json_encode($sql_results);
However, I want to add other data before I parse it as JSON. But I'm not sure how.
$sql_results basically gets me a list of post ID's, title and category.
It looks like this in var_dump (this is just the first row):
array(1)
{
[0]=> object(stdClass)#2737 (7)
{
["ID"]=> string(4) "2700"
["post_title"]=> string(18) "The compact helmet"
["category"]=> string(5) "Other"
}
}
Now to start with something easy, I'd like all associative arrays inside the object to have the extra key-value. I tried the following but got an error:
500 Internal error.
foreach($sql_search as $key => $value)
{
$value['pic_img'] = "test";
$sql_search[$key]=$value;
}
$result=$sql_search;
$sql_results = array(1)
{
[0]=> object(stdClass)#2737 (7)
{
["ID"]=> string(4) "2700"
["post_title"]=> string(18) "The compact helmet"
["category"]=> string(5) "Other"
}
}
foreach($sql_results as $key=>$value)
{
$value->solution = 'good';
$sql_results[$key]=$value;
}
$result=$sql_results;
var_dump($result);
$test = array ( array("ID"=>"35", "name"=>"Peter", "age"=>"43"),
array("ID"=>"34", "name"=>"James", "age"=>"19"), array("ID"=>"31", "name"=>"Joe", "age"=>"40") );
foreach($test as $key=>$value)
{
$value['solution'] = 'good';
$test[$key]=$value;
}
$result=$test;
var_dump($result);
This is the problem:
I have the following array (from $wpdb->get_results()):
array(6) {
[0]=> array(1) {
[0]=> string(7) "1102006"
}
[1]=> array(1) {
[0]=> string(7) "1102006"
}
[2]=> array(1) {
[0]=> string(7) "8092007"
}
[3]=> array(1) {
[0]=> string(8) "23062012"
}
[4]=> array(1) {
[0]=> string(8) "29072000"
}
[5]=> array(1) {
[0]=> string(8) "30082008"
}
}
And I would like to find the lowest integer from 10,000 on that is NOT in this array. In this case the answer would be 10,000 as 10,000 is not in the array.
Thanks
This is how I interpreted your question.
Starting from 10000, find the first available number that is NOT within your data array.
<?php
$data = array(
array('1102006'),
array('1102006'),
array('8092007'),
array('23062012'),
array('29072000'),
array('30082008')
);
// flatten the array to a single dimension
function flatten(&$v) { $v = $v[0]; }
array_walk($data, 'flatten');
// minimum number
$num = 10000;
// while a value has not been found
while (!isset($value))
{
// check if the current number is in our data (exclusion list)
if (array_search($num, $data) === false)
$value = $num;
// increment for our next search
$num++;
}
echo $value;
If you are just after the minimum value in that array, flatten the array in the previous answer and use min:
echo min($data);