I have created an Array but I am not getting the correct output, I am just wondering where I made error in code below
The Out put should display the name as below
John
James
Jonny
Jeni
<?php
$multiArray = Array(
Array("id" => 1, "name" => "John"),
Array("id" => 2, "name" => "James"),
Array("id" => 3, "name" => "Jonny"),
Array("id" => 4, "name" => "Jeni"));
$tmp = Array();
foreach($multArray as &$ma)
$tmp[] = &$ma[name];
array_multisort($tmp, $multiArray);
foreach($multiArray as &ma)
echo $ma["name"]."<br/>";
?>
What are you trying to achieve here? Are you trying to only print the content of the array?
Just do it like this is enough
<?php
$multiArray = Array(
Array("id" => 1, "name" => "John"),
Array("id" => 2, "name" => "James"),
Array("id" => 3, "name" => "Jonny"),
Array("id" => 4, "name" => "Jeni")
);
foreach($multiArray as $ma)
{
echo $ma['name'] . "<br>";
}
?>
Also I noticed that in your codes, there is a lot of typo there.
This is your codes after I fixed all the typos:
<?php
$multiArray = Array(
Array("id" => 1, "name" => "John"),
Array("id" => 2, "name" => "James"),
Array("id" => 3, "name" => "Jonny"),
Array("id" => 4, "name" => "Jeni")
);
$tmp = Array();
foreach($multiArray as $ma)
$tmp[] = $ma['name'];
array_multisort($tmp, $multiArray);
foreach($multiArray as $ma)
echo $ma["name"]."<br/>";
?>
Output:
James
Jeni
John
Jonny
Yes the output is different from what you want. this is because you sort the array using array_multisort. read: http://php.net/manual/en/function.array-multisort.php
Related
How can I sum an especial index in array for example I wanna sum -age(it's an index) in this array:
$users = array(
"user1" => array("name" => "Loghman Avand", "age" => 26),
"user2" => array("name" => "Sara Alavi", "age" => 34),
"user3"=> array("name" => "Hossein Ahmadi", "age" => 3)
);
Use array_column() and array_sum():
$ages = array_column($users, 'age');
echo 'Sum is: ', array_sum($ages);
This is ignoring the fact your keys in the outer array are the same.
First thing is your above array is invalid. Because same array key ('users') is used mode than one time.
Check this below code
$users = array("user1" => array("name" => "Loghman Avand", "age" => 26), "user2" => array("name" => "Sara Alavi", "age" => 34), "user3" => array("name" => "Hossein Ahmadi", "age" => 3));
$age_sum = 0;
foreach($users as $user){
$age_sum += $user['age'];
}
echo $age_sum;
I've that proceed this array in PHP
array(
"id" => 1,
"name" => "Carlos"
"other" => array("key" => "Hello")
),
array(
"id" => 3,
"name" => "Carlos"
"other" => array("key" => "Hello")
),
array(
"id" => 2,
"name" => "Carlos"
"other" => array("key" => "Hello")
)
and I need to order by "id". I've try it using usort and many multidimensional solutions but doesn't work for me.
I used that:
$price = array();
foreach ($inventory as $key => $row)
{
$price[$key] = $row['price'];
}
array_multisort($price, SORT_DESC, $inventory);
But doesn't work because my array has many dimentions.
$departamento = $this->Departamentos->get($id, [
'contain' => [
'Asignaturas.Mallas',
'Asignaturas.Secciones.Perfiles',
'Asignaturas.Secciones.Mallas.Carreras',
'Unidades'
]
]);
That is my query in Cakephp. I need to order by Secciones.id
I used Hash::sort
http://book.cakephp.org/3.0/en/core-libraries/hash.html
And works fine for me ;)
I have this array:
array(
"tour_0" => 1446,
"tour_1" => 1471,
"date-from-1471" => "2014-08-07",
"date-to-1471" => "2014-08-15",
"tour_2" => 30,
"date-from-30" => 2014-08-01,
"date-to-30" => 2014-08-05,
"tour_3" => 10
)
Now, i need it to be sorted to this:
array(
"0" => array("ID" => 1446),
"1" => array("ID" => 1471, "from" => "2014-08-07", "to" => "2014-08-15"),
"2" => array("ID" => 30, "from" => "2014-08-07", "to" => "2014-08-15"),
"3" => array("ID" => 10),
)
How can i accomplish this thing?
I've tried all sorts of things but i can't seem to figure this one out...
Thank's and sorry about the title but i just don't know how to describe it.
How about this?
$ret = [];
foreach($inputArray as $key => $value) {
if (preg_match('/^tour_([0-9]+)/', $key)) {
$ret[$value] = ["ID" => $value];
}
if (preg_match('/date-from-([0-9]+)/', $key, $matches)) {
$ret[$matches[1]]["from"] = $value;
}
if (preg_match('/date-to-([0-9]+)/', $key, $matches)) {
$ret[$matches[1]]["to"] = $value;
}
}
print_r($ret);
/*
Array
(
"1446" => Array ("ID" => 1446),
"1471" => Array ("ID" => 1471, "from" => "2014-08-07", "to" => "2014-08-15"),
"30" => Array ("ID" => 30, "from" => "2014-08-01", "to" => "2014-08-05"),
"10" => Array ("ID" => 10)
)*/
Close enough? (it is quite trival change the keys of the array, considering they are in order (0, 1, 2, 3, ...), if they are not, maybe you can save the order also (in another item of the subarray) and recompose again once this array is formed)
i have this multi array "$marray wich has inside some array with some similar key = value and some not look like this :
$marray = array(
array("id" => "1", "be_pro" => 6, "name" => "a1", "service" => 4a),
array("id" => "2", "be_pro" => 6, "name" => "a1", "service" => 4d),
array("id" => "3", "be_pro" => 4, "name" => "a4", "service" => 3d),
array("id" => "4", "be_pro" => 4, "name" => "a4", "service" => 3s),
array("id" => "6", "be_pro" => 4, "name" => "a4", "service" => 34),
array("id" => "8", "be_pro" => 3, "name" => "a3", "service" => 4r),
array("id" => "8", "be_pro" => 3, "name" => "a3", "service" => 4d)
);
So i would like to get new arrays with "id", "be_pro" and "name" once then "service" plus "service" from next array till "be_pro" in the new array is different , so if is different put in the next array.
How should i do this?
what i need is print a multi array and within and array with every row with similar be_pro
This help me:
// first get all bepro_id and put in array
foreach($all_rows as $service){
$bepros[$service['bepro_id']]++;
}
//var_dump($bepros);
//Then for each be pro_id print every row , so print every service , then for be pro_id,id,name just print the first key "[0]" the array inside $new_array
foreach ($bepros as $key=>$bepro){
if(isset($new_bep)){
$x = 0 + $new_bep;
}else{
$x = 0;
}
$new_array[] = array_slice($all_rows,$x,$bepro);
// get where let before
$new_bep=$x + $bepro;
}
I have an array of locations. Each of these locations can have child locations. Each of the child locations can also have children, and so on:
$locations = array(
array("id" => 1, "parent_id" => 0, "name" => "England"),
array("id" => 2, "parent_id" => 0, "name" => "Scotland"),
array("id" => 3, "parent_id" => 0, "name" => "Ireland"),
array("id" => 4, "parent_id" => 0, "name" => "Wales"),
array("id" => 5, "parent_id" => 1, "name" => "East England"),
array("id" => 6, "parent_id" => 1, "name" => "London"),
array("id" => 7, "parent_id" => 6, "name" => "West London"),
array("id" => 8, "parent_id" => 6, "name" => "East London"),
array("id" => 9, "parent_id" => 1, "name" => "East Midlands"),
array("id" => 10, "parent_id" => 9, "name" => "Derbyshire")
);
I want to re-structure this array so that the children are arrays of the parent. Something like this (untested):
$locations = array("id" => 1, "parent_id" => 0, "name" => "England", "children" => array(
array("id" => 5, "parent_id" => 1, "name" => "East England"),
array("id" => 6, "parent_id" => 1, "name" => "London", "children" => array(
array("id" => 7, "parent_id" => 6, "name" => "West London"),
array("id" => 8, "parent_id" => 6, "name" => "East London")))));
This is so I can then print them out using indents like so:
LOCATIONS
England
- East England
- London
-- West London
-- East London
- East Midlands
-- Derbyshire
Scotland
Ireland
Wales
I have tried several ways, like grouping them by parent ID, but I just can't work out the logic for this, and there may be a better way of doing it (recursion, perhaps?).
Many thanks.
Hi perhaps this will help you, i just wrote it to quickly convert a mysql result containing a parent_id into a usable data hierarchy. Your input array should also work. It's just a couple of lines with two basic loops. No recursion required. Some comments included:
<?php
$max = count($inputArray);
$tree = array();
$flat = array();
// Create a flat hierarchy array of all entries by their id
for ($i = 0; $i < $max; $i++) {
$n = $inputArray[$i];
$id = $n['page_id'];
$flat[$id] = $n;
}
// Then check all those entries by reference
foreach ($flat as $key => &$child) {
// Add a children array if not already existing
if (!isset($child['children']))
$child['children'] = array();
$id = $child['page_id'];
$pid = $child['parent_id'];
// If childs parent id is larger then zero
if ($pid > 0) {
// Append it by reference, which means it will reference
// the same object across different carriers
$flat[$pid]['children'][] = &$child;
} else {
// Otherwise it is zero level, which initiates the tree
$tree[$id] = &$child;
}
}
$tree = array_values($tree); // Indices fixed, there we go, use $tree further
?>
So notice the by '& reference' characters. They do all the work allowing the tree to be built from the flat array by pointing to the same objects.
See this excellent anwser from Bill Kalwin.
As well as his presentation on "Models for hierarchical data (PHP and SQL)"
Also, you may be interested in storing directly data as a "nested set" structure in your DB.