0

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..

asked Jan 2, 2014 at 7:40
1
  • 1
    in while : $ret[] = $names['names'], and after while : $names = implode(',', $ret) Commented Jan 2, 2014 at 7:51

2 Answers 2

1
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;
 )
);
answered Jan 2, 2014 at 7:57
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you David, using the "implode" the way you did allow me to send all of the information in a single array with " , " separated, I have sent an email to the developers of that API pointing them to your answer, they kindof like the idea of having the information sent with " , " separated or any other symbol, but to be honest that looks kind of clumsy... now this is just a little block from a multidimensional array... so thank you your answer was helpful
No proble. BTW, ryrysz posted the same thing in a comment before I typed my answer. I'd at give it an upvote also if it resolved your issue.
0

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.

answered Jan 2, 2014 at 8:18

1 Comment

Hello Leonix, the API only "GET's" and "return" certain information base on what you send, the thing is that API is kind of new, so you can only send information with specific format and receive results the same way very specific, before I posted here, I build the array the same way you did but the API return an error.. so I was looking for a way to send all of the information in a single array, I never thought that it was possible to use "implode" the way David did... and it worked...

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.