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';
Related
I am creating a response from my own webhook.
Now i wanted to send the suggestions array, but am struggling with creating an array in an array. How does these need to be set up?
$jsonResponse = json_encode(array(
"session" => array(
"params" => array(
"antA" => "Hello Answer A",
"antB" => "Hello Answer B",
"extrainfo" => "This is some extra information"
)
),
"prompt" => array(
"override" => false,
"firstSimple" => array(
"speech" => "<speak>".$speech."</speak>"
),
"suggestions" => array(
"title" => "aa",
"title" => "bb",
"title" => "cc"
),
)
));
The issue is that prompt.suggestions is specified to take an indexed array of Suggesion objects, that is, an array that maps from numbers to the objects (or just a list, where the numbering is assumed). But you are providing an associative array - that is, mapping a property name to something. Furthermore, your associative array is naming everything the same. Php uses similar syntax for indexed arrays and associative arrays, so it can sometimes be unclear what you actually need.
In this case, that part of your code should probably look something more like this:
"suggestions" => array(
array("title" => "aa"),
array("title" => "bb"),
array("title" => "cc")
)
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 would like to add an array to within an existing array.
I am tryin to use array_push which works as long as i dont try to assign a key to the array (if i try to add a key i get a syntax error... :-()
This is my initial array:
$ResultArray = array(
"TransactionDate" => "$TransactionDate",
"tx"=>array(
"0"=>array(
"TxIndex" => "$TxIndex",
"value" => "$Value",
"PaymentConfirmedCount" => "$PaymentConfirmedCount"
),
"1"=>array(
"TxIndex" => "$TxIndex",
"value" => "$Value",
"PaymentConfirmedCount" => "$PaymentConfirmedCount"
)
)
);
i would then like to add:
$ArrayTOAdd = array(
"0"=>array(
"TxIndex" => "$TxIndex",
"value" => "$Value",
"PaymentConfirmedCount" =>
"$PaymentConfirmedCount"
)
);
if I try:
array_push($ResultArray->tx, $ArrayTOAdd);
BUT this does not work and results in a warning of "array_push() [function.array-push]: First argument should be an array"
if i try this :
array_push($ResultArray, $ArrayTOAdd);
it just adds the array but not to $ResultArray->tx
Any suggestions would be greatly welcomed!
You have to access the element in the array with $ResultArray["tx"] and not $ResultArray->tx. The second one is for the access to members in a php class. So an
array_push($ResultArray["tx"], $ArrayTOAdd);
should work.
I made this fictional membership list. I'd like to maintain the order of the sub arrays (class year) while sorting by descending key within each sub array.
$pledges = array(
'smith' => "Joe Patterson",
'jones' => "Robert Nelson",
'davis' => "Jimmy Davis",
'carpenter' => "Mike Carpenter");
$sophomores = array(
'ford' => "Kevin Ford",
'gomez' => "Pedro Gomez",
'miller' => "Jack Miller",
'pullman' => "Lucas Pullman");
$juniors = array(
'bradford' => "Nicholas Bradford",
'daniels' => "Robert Daniels",
'soren' => "Jon Soren",
'cooper' => "Harrison Cooper");
$seniors = array(
'mcdonald' => "Casey McDonald",
'witten' => "Tim Witten",
'session' => "Benjamin Sessions",
'redding' => "Jack Redding");
How do I do that? Thanks a bunch.
The PHP manual has a good example of what you're looking to do.
http://www.php.net/manual/en/function.ksort.php#98465
by using ksort function of PHP you can sort array key wise.
ksort($pledges);
array_reverse($pledges);
and like wise for others....
what you have posted is single dimension array and be aware that ksort will be only applicable to single dimension array.
I'm storing images links into the database separating them with ,, but I want to transform this string into an array, but I'm not sure how to do it.
So my array looks like this:
$array = array(
"name" => "Daniel",
"urls" => "http:/localhost/img/first.png,http://localhost/img/second.png"
);
So I'd like to have it in the following form:
$array2 = array(
"name" => "Daniel",
"urls" => array("http:/localhost/img/first.png",
"http://localhost/img/second.png" )
);
I haven't been PHP'ing for a while, but for that simple use-case I would use explode.
$array['urls'] = explode(',', $array['urls']);
Uncertain if I interpreted your question correct though?
You can use array_walk_recursive like in the following example.
function url(&$v,$k)
{
if($k=='urls') {
$v = explode(",",$v);
}
}
$array = array(
"name" => "Daniel",
"urls" => "http:/localhost/img/first.png,http://localhost/img/second.png"
);
array_walk_recursive($array,"url");
You can check the output on PHP Sandbox