I have a query which returns several thousands objects from my database. The result set is an array of associative arrays. An example would be something along the lines of:
Array(
Array(
"id" => 500,
"name" => "Bob"
),
Array(
"id" => 2,
"name" => "Cindy"
),
Array(
"id" => 200,
"name" => "Jane"
)
);
In this case I'd need to be able to filter/sort this array to retrieve the id of 500.
Here's one way to do it:
Get the ids into an array (using array_column())
Get the highest value in the array (using max())
This should do the trick:
echo max(array_column($array, 'id'));
Demo
Title sounds a bit convoluted but the title itself is pretty self-descriptive I think.
Assume that I have an associative array like this:
$data['blog_info'] = array(
"title" => "Adventure",
"author" => "Yo"
);
Now, I would like add to the current key 'blog_info" another set of key => value array. so the result should be :
$data['blog_info'] = array(
"title" => "Adventure",
"author" => "Yo",
"ISBN" => "23423498"
);
so for example I would like to add "ISBN" => "23423498" inside this 'blog_info' key. How am I able to achieve this? (but by going like $data['blog_info'].push("ISBN" => "23423498") etc?)
The below will achieve it ($data['blog_info'] is just an array).
$data['blog_info']['ISBN'] = '23423498';
In PHP you don't need the curly braces { and } in this context.
$data['blog_info'] = array(
"title" => "Adventure",
"author" => "Yo"
);
Try something like
$data['blog_info']['ISBN'] = '23423498';
I have been trying to figure this out but keep getting confused with code examples.
How can I add a single element to the array below.
$datafields = array(
"helpdesk_ticket" => array(
"subject" => $subject,
"description" => $notes,
"email" => "joe#email.net",
"priority" => $priority2,
"ticket_type" => $type,
"viewed" => $viewed
)
);
So for instance I want to add:
"status" => $status
This is just a simple array assignment.
$datafields['helpdesk_ticket']['status'] = $status;
For more info read the manual about arrays
I have the following array from an API
$arrs = array(
"id" => $ids,
"names" => $names,
"description" => $desc,
"picture_url" => $p_img;
);
The API require this information to work unfortunately I can only have one array per request, so I send this question to the developers:
How can I list multiple item such as:
$arrs= array(
"items1" => array (
"id" => $ids,
"names" => $names,
"description" => $desc,
"picture_url" => $p_img;
)
"items2" => array (
"id" => $ids,
"names" => $names,
"description" => $desc,
"picture_url" => $p_img;
)
"items3" => array (
"id" => $ids,
"names" => $names,
"description" => $desc,
"picture_url" => $p_img;
)
));
And they told me that at the moment is not possible, so, the "important" part of this array is the "names", when it is use with a single item there is no problem I get a single name, done, no problem, but what if I have multiple names? I can send multiple request but that will be seen as a flood or something like... just imaging 300 names = 300 request in one second or so... sure I can put a pause per request but is not efficient...
the API will read something like this...
"id" => 654,
"names" => "John", // <-- Lets look at this...
"description" => "Fancy desc...",
"picture_url" => "http"//domain.com/assets/user/654/av_654_dd.jpg";
So before I output the array I have an SQL Query with a while to display the information...
while ($names = $listnames->fetch_assoc()) {echo $names['names']. ', ';}
This will display... John, Karl, Lisa, Mark... so this same structure I'd love to put it into my array... the thing is I can't put a while after the => ... that would be silly and it wont work...
"id" => 654,
"names" => "John, Karl, Lisa, Mark", // <-- Lets look at this...
"description" => "Fancy desc...",
"picture_url" => "http"//domain.com/assets/user/654/av_654_dd.jpg";
if I need only one name then there is not problem... but in this case I need to put all of the name as a value, so, how can get the result from a WHILE loop.... so that I can use that result elsewhere...
Thank you for taking the time..
while ($names = $listnames->fetch_assoc()) {
$name_array[] = $names['names'];
}
$arrs=array(
"items1" => array (
"id" => $ids,
"names" => implode(', ', $name_array),
"description" => $desc,
"picture_url" => $p_img;
)
);
We don't know how you access to the API.
If this is a REST API, you are able to do only what the developers planned.
If this is a framework or another library, you may edit it.
And you should receive your results like theses:
$arrs = array(
array(
"id" => 1,
"names" => 'MyName',
"description" => 'MyDesc',
"picture_url" => 'MyPic',
),
array(
"id" => 2,
"names" => 'MyName',
"description" => 'MyDesc',
"picture_url" => 'MyPic',
),
);
So if you have full access to the SQL results, to get all results' data, you could do:
$results = array();
while( $row = $listnames->fetch_assoc() ) {
$results[] = $row;
}
If you want to set several possible name in input, the developers should develop that is possible with array of values.
For names, they just have to code: "LIKE '".implode("', LIKE '", $names)."'"
They could also add '%' to allow more values, e.g. 'John' for 'Johnny'.
I think we need more informations to really help you.
I want to output users via a json object but when I try to output their songs list it only outputs the last one. I want to get this list into an array.
this is my array push while looping through users,
array_push($arrayUsers, array(
'username' => $user['username'],
'id' => $user['_id'],
'favSongs' => array(
'title' =>'song1',
'title' =>'song2'
)
)
);
but this is what I get back (missing song title),
[{"username":"asdfasdfasd","id":{"$id":"4f58d7227edae19c02000000"},"songs":{"title":"song2"}}]
I want it to output the songs like this, but am confused how to get it to do this using PHP:
"songs":[{"title": "song1"}, {"title": "song2"}]
'favSongs' => array(
'title' => 'song1',
'title' => 'song2'
)
PHP will replace the 'title' key with the last one declared.
"songs":[{"title": "song1"}, {"title": "song2"}]
This is an array of objects, so in PHP it needs to be an array of arrays.
'favSongs' => array(
array('title' => 'song1'),
array('title' => 'song2')
)