This question already has answers here:
PHP - Append elements to array
(3 answers)
Closed 8 years ago.
I'm trying to populate all the links in my database into an array with a key something like this:
$link = array(99999 => "http://link1.com", 111111 => "http://link2.com");
Which returns:
Array ( [99999] => http://link1.com [111111] => http://link2.com )
How can i get the array to look like that when going through a loop, Here is my code:
while($row = mysql_fetch_array($query)) {
$link[] = "$row[r_id] => $row[r_link]";
}
It returns:
Array ( [0] => 24004 => http://link1.com [1] => 30554 =>
http://link2.com
But i don't wan't it to be like this, How can i get it the same as the first example while going through a loop ?
while($row = mysql_fetch_array($query)) {
$link[$row['r_id']] = $row['r_link'];
}
$link[$row['r_id']] = $row['r_link'];
Related
This question already has answers here:
Looping a multidimensional array in php
(3 answers)
How to load Google Maps in wpf window?
(2 answers)
Closed 5 years ago.
i have arrays like this
Any idea how to access #items subset with foreach or somthing fast like this?
Array
(
[0] => Array
(
[#theme] => item_list
[#items] => Array
(
[0] => Array
(
[nid] => 1
[changed] => 1514034947
[title] => TITLE 1
)
[1] => Array
(
[nid] => 2
[changed] => 1514034947
[title] => TITLE 2
)
)
)
[1] => Array
(
[#theme] => pager
)
BEST
As your elements are located below 3 levels so you need to use foreach thrice like below:
foreach($data as $items_list){
foreach($items_list[#items] as $items){
foreach($items as $val){
echo $val['nid'];
}
}
}
You can simply count array everytime and add condition before foreach loop to check if further array exists or not.
Like you can count main array and add condition with while loop and then use foreach like below:
$count = 0;
while($count<count($array)){
foreach($array[$count]['#items'] as $items ){
foreach($items as $val){
echo $val['nid'];
}
}
$count = $count+1;
}
Before doing anything, its better if you debug your multiple arrays like below:
echo "<pre>"; print_r($array); die;
You can use a nested foreach to get the final item list from this array object...
foreach($data as $items_list)
{
foreach($items_list[#items] as $items)
{
// do something here....
}
}
Assuming the array structure is exactly as you posted here is something you can experiment with. I don't know what you want to do with each item so this example just prints out the elements of each one:
foreach( $array as $outerarray )
{
foreach( $outerarray['#items'] as $item )
{
echo "{$item['nid']} {$item['changed']} {$item['title']}\n";
}
}
This question already has answers here:
Get value without knowing key in one-pair-associative-array
(4 answers)
Closed 5 years ago.
i have this scenario of having a mixed response from a server and i need to process its data in PHP
Array
(
[14424174] => Array
(
[0] => Array
(
[id] => 45
[nm] => This is a driver name
[ph] => 5454545
)
)
)
I want to access id, nm, ph values
but i had no luck cause this index number (14424174) is unknown to me, so i need to first store this index and then parse the array
Use a nested foreach():
foreach($arr as $i => $sub_arr)
{
foreach($sub_arr as $sub_i => $sub_sub_arr)
{
$id = $sub_sub_arr['id'];
$nm = $sub_sub_arr['nm'];
$ph = $sub_sub_arr['ph'];
}
}
You can use the following pattern:
foreach($array as $key=>$val) {
//get the id:
var_dump($key)//14424174
$nm = $val[nm];
$ph = $val[ph];
}
This question already has answers here:
Reverse an associative array with preserving keys in PHP
(4 answers)
Closed 5 years ago.
I am developing a new website, and I have a quetion.
Input array:
Array ( [1319] => ####,[1316] => ###)
I have an array and I want to revese him, after the reverse the array would be like this:
Expected output:
Array ( [1316] => ###,[1319] => ####)
but when i'm using array_reverse function, it doesnt work for me, I got this array:
Array ( [0] => ###,[1] => ####)
Why it is happen?
For preserving keys you just pass second parameter to true in array_reverse.
Try this code snippet here
$array=Array ( 1319 => "####",1316 => "###");
print_r(array_reverse($array,true));
you can try this:
$a = []; //your array
$keys = array_keys($arr);
$values = array_values($arr);
$rv = array_reverse($values);
$newArray = array_combine($keys, $rv);
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
JSON Parsing with PHP
I'm hoping for a little assistance on this. I trying to take what's in my $data variable:
[{"Product":"Internal",
"Rank":"1",
"Number":"1234"},
{"Product":"External",
"Rank":"1",
"Number":"5678"}]
and turn it into an associative array that is something like this:
Product[0] => Internal,
Rank[0] => 1,
Number[0] => 1234,
Product[1] => External,
Rank[1] => 1,
Number[1] => 5678
The closest I've been able to get is using the following code:
$del='/[{: ,}]/';
$data = preg_split($del,$data);
print_r($data);
Which gives me something like this:
Array ( [0] => [ [1] => "Product" [2] => "1 [3] => 1" [4] => "Rank"
[5] => "1"
Any suggestions would be greatly appreciated. Thanks for your time!
If you know that $data is in JSON format, you could just do this:
$data = json_decode($data);
Then you want to iterate through the array of objects and transmogrify them:
$output = array();
foreach($data as $index=>$object) {
foreach($object as $name=>$value) {
$output[$name][$index] = $value;
}
}
Firstly, use json_decode($data,TRUE), this will make your life easier, giving you nice array to transform in what you want.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
in_array() and multidimensional array
Got the following array returned from a database using this code:
$skus = array();
$result = mysql_query($sql);
if($result){
while($rows = mysql_fetch_array($result)){
$skus[]=$rows;
}
}
Results:
Array (
[0] => Array {
[0] => PUBELI
[group_sku] => PUBELI
)
[1] => Array (
[0] => PUBESSENTIALS
[group_sku] => PUBESSENTIALS
)
[2] => Array (
[0] => PUBMGRPROGROUPED
[group_sku] => PUBMGRPROGROUPED
)
[3] => Array (
[0] => PUB25GROUPED
[group_sku] => PUB25GROUPED
)
)
I'm looking for this value using in_array:
if (in_array('PUBESSENTIALS', $skus))
and it returns false. Am I doing this correctly?
Why would the array values not be enclosed in quotes if the values in the DB are strings?
Don't use PHP if you may do something with MySql!
Try this solution:
$sql = "SELECT * FROM table WHERE str = 'PUBESSENTIALS'"; // some query just add WHERE
$result = mysql_query($sql);
if($result) $Row = mysql_fetch_array($result)
Assuming $skus is the full array shown above, then 'PUBESSENTIALS' would not be in $skus, because $sku's contains child arrays.
However, in_array('PUBESSENTIALS', $skus[1]) would return true.
try looping through each $skus element, and then checking that child element for in_array(value, childArray)
You are only looking into the first array, and not any other array. You should look through each to test every sub-array. Something like this could do the job:
foreach($skus as $sku) {
if (in_array('PUBESSENTIALS', $sku)) {
return true;
}
}