I got a query result like this : -
Array(
[0] => Array
(
[0] => Array
(
[id] => 1
[name] => Japan
)
)
[1] => Array
(
[0] => Array
(
[id] => 2
[name] => Nepal
)
)
}
How can I convert it to
Array
(
[0] => Japan
[1] => Nepal
)
The query used to get first array is 'select id , name from country;'
Please help me.
foreach ($element as $array) {
$element = $element[0]["name"];
}
Or,
function extractName($element) {
return $element[0]["name"];
}
$newArray = array_map("extractName", $array);
Extract!
$newArray = Set::extract($oldArray, '{n}.0.name');
More here.
Related
How can i remove the first dimansion of a multidemensional Array without loosing the keys?
i have an Array that have multiple arrays inside
the firstkey is a Date and the secondkey is the hour.
my Output is:
Array
(
[0] => Array
(
[firstkey] => Array
(
[secondkey] => Array
(
[0] => 7
[1] => 8
)
)
)
[1] => Array
(
[firstkey] => Array
(
[secondkey] => Array
(
[0] => 7
[1] => 8
)
)
)
)
and i want this:
Array
(
[firstkey] => Array
(
[secondkey] => Array
(
[0] => x
[1] => y
[2] => z
[3] => r
)
)
)
i also tried array_merge_recursive() but instead of putting the values to the secondkey it creates a new array with an incremental key
okay found a solution on:
PHP : multidimensional array merge recursive
function array_merge_recursive_ex(array $array1, array $array2)
{
$merged = $array1;
foreach ($array2 as $key => & $value) {
if (is_array($value) && isset($merged[$key]) && is_array($merged[$key])) {
$merged[$key] = array_merge_recursive_ex($merged[$key], $value);
} else if (is_numeric($key)) {
if (!in_array($value, $merged)) {
$merged[] = $value;
}
} else {
$merged[$key] = $value;
}
}
return $merged;
}
and with this function i can merge that mutlidemensional array without loosing keys:
$newarray =[];
foreach($array as $firstkey){
$newarray = array_merge_recursive_ex($newarray, $firstkey);
}
I have this given array ,
How can get the value of email and status in loop .
Array
(
[0] => Array
(
[brandlogo_id] => 1
[email] => upendtu#gmail.com
[status] => 2
[created_time] => 2017-11-13 14:00:10
[update_time] => 2017-11-13 14:00:10
)
[1] => Array
(
[brandlogo_id] => 2
[email] => upen3592#gmail.com
[status] => 1
[created_time] => 2017-11-13 13:58:46
[update_time] => 2017-11-13 13:58:46
)
[2] => Array
(
[brandlogo_id] => 3
[email] => ankigupta9492#gmail.com
[status] => 1
[created_time] => 2017-11-13 13:57:07
[update_time] => 2017-11-13 13:57:07
)
)
please let me know how can i get this value
thank you.
Be it any language, the method/ also will be more or less the same apart from the syntax. Search the syntax for the language you are using to parse JSON data.
Giving examples.
For Objective - C:
NSArray *resultDataSet=[NSArray array];
while([results next]) {
NSString *name = [results stringForColumn:#"brand_name"];
resultDataSet = [resultDataSet arrayByAddingObject:name ];
}
For Python :
for array in data:
print array['email']
For PHP :
foreach($arr as $item) { //foreach element in $array
$stat = $item['status']; //etc
}
Hope that helps.
Assuming that you are using PHP or python (as this is an associative array) and the name of the array is "arr".
This code shall do the job.
For PHP
foreach($arr as $subarray) {
echo $subarray["email"]." ".$subarray["status"]."<br>";
}
For Python
for subarray in arr:
print subarray["email"], " ", subarray["status"]
It will display output similar to
xyz#gmail.com 2
abc#yahoo.com 3
Array looks like below :
Array
(
[0] => Array
(
[memberid] => 5203
)
[1] => Array
(
[memberid] => 494
)
[2] => Array
(
[memberid] => 1053
)
[3] => Array
(
[memberid] => 1081
)
)
How can i find-out the outer array index (such as 0,1,2) using inner array.
You can iterate the outer array, and save the key if the current inner array value matches like this:
$outerKey = null;
foreach($outerArray as $key => $member){
if($member['memberid'] == 1081){
$outerKey = $key;
break;
}
}
echo $outerKey; // it will print 3
I have this array from a sql query :
[0] => Array
(
[T1] => Array
(
[First] => A
[Second] => Apples
[LastChild] => F
)
[0] => Array
(
[LastChildNb] => 23
)
)
I would like to have this result :
[0] => Array
(
[0] => Array
(
[First] => A
[Second] => Apples
[LastChild] => F
[LastChildNb] => 23
)
)
How do I do this ? I think I should use "hash::combine", but what would the code be ?
You could do something like this, with $arr being your array above:
$arr = array_reduce($arr, function(&$arr, $v) {
return array_merge($arr, (array) $v);
}, array());
You can do
array_merge($arr[0]['T1'], $arr[0][0])
Where $arr is defined as follows:
$arr = [0 => Array
(
'T1' => Array
(
'First' => 'A',
'Second' => 'Apples',
'LastChild' => 'F'
),
0 => Array
(
'LastChildNb' => 23
)
)];
For multiple records in $arr, you can simply cycle over all records and do this merge manually.
However, assuming you're getting the array as a result of a find() method, I suggest you consider using T1__LastChildNb as an alias within the 'fields' of your condition. Simply said, if you have a find like following:
$this->T1->find('all', ['fields' => 'T1.*, (SOME SUBQUERY) AS LastChildNb']);
then modifying it to be
$this->T1->find('all', ['fields' => 'T1.*, (SOME SUBQUERY) AS T1__LastChildNb']);
might be what you're looking for since it will return desired array directly (tested on 2.6).
Let me know if you're interested in more information.
I have an associative array in following format.I need to sort the array in descending order and sort the array with same value as ascending order.
$numArray = array();
$numArray[0]['Word'] = 'One';
$numArray[0]['Number'] = 10.00;
$numArray[1]['Word'] = 'Two';
$numArray[1]['Number'] = 10.00;
$numArray[2]['Word'] = 'Three';
$numArray[2]['Number'] = 10.00;
$numArray[3]['Word'] = 'Four';
$numArray[3]['Number'] = 30.00;
$numArray[4]['Word'] = 'Five';
$numArray[4]['Number'] = 20.00;
$numArray[5]['Word'] = 'Six';
$numArray[5]['Number'] = 10.00;
$numArray[6]['Word'] = 'Seven';
$numArray[6]['Number'] = 20.00;
$numArray[7]['Word'] = 'Eight';
$numArray[7]['Number'] = 20.00;
Please help me to sort the array in following order.
Array
(
[0] => Array
(
[Word] => Four
[Number] => 30
)
[1] => Array
(
[Word] => Five
[Number] => 20
)
[2] => Array
(
[Word] => Seven
[Number] => 20
)
[3] => Array
(
[Word] => Eight
[Number] => 20
)
[4] => Array
(
[Word] => One
[Number] => 10
)
[5] => Array
(
[Word] => Two
[Number] => 10
)
[6] => Array
(
[Word] => Three
[Number] => 10
)
[7] => Array
(
[Word] => Six
[Number] => 10
)
)
foreach ($numArray as $key => $row) {
$array[$key] = $row['Number'];
}
array_multisort($array, SORT_DESC, $numArray);
This little php code will at least sort your array desc according to the Number.
guess thats half of what you want .