I have an array like this:
Array
(
[0] => Array
(
[settingID] => 1
[name] => audioCueDistance
[setValue] => false
)
[1] => Array
(
[settingID] => 2
[name] => audioCueDistanceToGo
[setValue] => true
)
[2] => Array
(
[settingID] => 3
[name] => audioCues
[setValue] => true
)
[3] => Array
(
[settingID] => 4
[name] => audioCueStyle
[setValue] => default
)
[4] => Array
(
[settingID] => 5
[name] => audioCueTime
[setValue] => true
)
[5] => Array
(
[settingID] => 6
[name] => isMetric
[setValue] => true
)
How can I get individual values from key for example, I would like to output the setValue of isMetric.
Thanks
foreach ($foo as $bar) {
if ($bar['name'] == "isMetric") {
// Use setValue here
}
}
From what I understand you want to do something like $myArray['isMetric']['setValue'].
As your array is not in that form you need to map it that way.
$myArray = array(
array(
'settingID'=>6,
'name'=>'isMetric',
'value'=>true
)
);
$myAssocArray = array_reduce($myArray, function($carry, $item){
$carry[$item['name']] = $item;
return $carry;
}, array());
echo $myAssocArray['isMetric']['setValue'];
Run this code here: https://repl.it/CZ3R
foreach($array as $element)
{
if($element['name'] = 'isMetric')
return $element['setValue'];
}
throw new \Exception('isMetric Not Found.');
Related
Array
(
[0] => Array
(
[hotel_id] => 79
[logo] => 1463466926-97157549.jpg
)
[1] => Array
(
[hotel_id] => 78
[logo] => 1463466942-15603675.jpg
)
[2] => Array
(
[hotel_id] => 77
[logo] => 1463466953-25200244.jpg
)
[3] => Array
(
[hotel_id] => 76
[logo] => 1463466967-62926110.jpg
)
[4] => Array
(
[hotel_id] => 75
[logo] =>
)
[5] => Array
(
[hotel_id] => 74
[logo] =>
)
)
Its my array values.
Here I send some values such as hotel_id & logo of that hotel..
But I should need to send the logo as image URL
i.e:,for this:1463466926-97157549.jpg
I need to send as
http://localhost/abservetech/laravel/abserve_crud_travelz/public/1463466926-97157549.jpg
By adding the path of that image..
And finally my array should be like this..
Array
(
[0] => Array
(
[hotel_id] => 79
[logo] => http://localhost/abservetech/laravel/abserve_crud_travelz/public/1463466926-97157549.jpg
)
[1] => Array
(
[hotel_id] => 78
[logo] => http://localhost/abservetech/laravel/abserve_crud_travelz/public/1463466942-15603675.jpg
)
[2] => Array
(
[hotel_id] => 77
[logo] => http://localhost/abservetech/laravel/abserve_crud_travelz/public/1463466953-25200244.jpg
)
[3] => Array
(
[hotel_id] => 76
[logo] => http://localhost/abservetech/laravel/abserve_crud_travelz/public/1463466967-62926110.jpg
)
[4] => Array
(
[hotel_id] => 75
[logo] => http://localhost/abservetech/laravel/abserve_crud_travelz/public/300x300.jpg
)
[5] => Array
(
[hotel_id] => 74
[logo] => http://localhost/abservetech/laravel/abserve_crud_travelz/public/300x300.jpg
)
)
Here, the last two hotels having null images.
For that I send an default image URL such as http://localhost/abservetech/laravel/abserve_crud_travelz/public/300x300.jpg
like this..
Someone could help me please..
Thanks in advance...
I have tried like this
$im =array();
foreach ($Roo as $key => $value)
{
$im[]=(\URL::to('').'/'.$value['logo']);
}
Here, \URL::to('').'/' is my path AND $Roo is my array
But,Instead it retrieve only the logo in separate array.
loop through your array and update your array as follows :
foreach ($array as $key => &$value) {
if ($value['logo'] != '') {
$value['logo'] = 'http://localhost/abservetech/laravel/abserve_crud_travelz/public/'.$value['logo'];
} else {
$value['logo'] = 'http://localhost/abservetech/laravel/abserve_crud_travelz/public/300x300.jpg';
}
}
print_r($array);
foreach($hotel_details as $key => $detail) {
if(!empty($detail['logo']) {
$detail['logo'] = 'http://localhost/abservetech/laravel/abserve_crud_travelz/public/'.$detail['logo']
$hotel_destails[$key] = $detail
} else {
$detail['logo'] = 'http://localhost/abservetech/laravel/abserve_crud_travelz/public/300x300.jpg'
$hotel_destails[$key] = $detail
}
}
Say your array name is $arr, so start traversing the whole array and replace your logo with new one if hotel_id is match.
Using the & you mean that the array is reference so what you change here must update in the main array, this is optional.
$path = 'http://localhost/abservetech/laravel/abserve_crud_travelz/public/';
$hotel_id = 79;
foreach($arr as $key => &$value){
if($value['hotel_id'] == $hotel_id){
$value['logo'] = $path.$value['logo'];
break;
}
}
print_r($arr);
Note: If you want to change the whole array then it will be very easy,
just remove the if condition.
There you should check about array_map() function.
With this entry array :
Array
(
[0] => Array
(
[hotel_id] => 0
[logo] => image_0.jpg
)
[1] => Array
(
[hotel_id] => 1
[logo] => image_1.jpg
)
[2] => Array
(
[hotel_id] => 2
[logo] => image_2.jpg
)
[3] => Array
(
[hotel_id] => 3
[logo] =>
)
[4] => Array
(
[hotel_id] => 4
[logo] => image_4.jpg
)
)
You can get this output array :
Array
(
[0] => Array
(
[hotel_id] => 0
[logo] => http://some_addr.tld/some_dir/some_sub_dir/image_0.jpg
)
[1] => Array
(
[hotel_id] => 1
[logo] => http://some_addr.tld/some_dir/some_sub_dir/image_1.jpg
)
[2] => Array
(
[hotel_id] => 2
[logo] => http://some_addr.tld/some_dir/some_sub_dir/image_2.jpg
)
[3] => Array
(
[hotel_id] => 3
[logo] => http://some_addr.tld/some_dir/some_sub_dir/300x300.jpg
)
[4] => Array
(
[hotel_id] => 4
[logo] => http://some_addr.tld/some_dir/some_sub_dir/image_4.jpg
)
)
By simply doing this :
$base = 'http://some_addr.tld/some_dir/some_sub_dir/';
$default = '300x300.jpg';
$res = array_map(
function($x) use ($base, $default)
{
if ( array_key_exists( 'logo', $x ) )
{
if ( !empty( $x['logo'] ) )
{
$x['logo'] = $base . $x['logo'];
}
else
{
$x['logo'] = $base . $default;
}
}
return $x;
}
, $arr);
Where $arr is the array containing your hotels, and $res will be the modified array.
you can do achieve this by following code:
$url = 'http://localhost/abservetech/laravel/abserve_crud_travelz/public/';
foreach ($arr as &$value) {
if ($value['logo'] != '') {
$value['logo'] = $url.$value['logo'];
} else {
$value['logo'] = $url;
}
}
How can I create a function to display an array as a tree. For example I want to obtain a decision tree which I want to walk until I get to the leafs based on the branch's values. I create the tree like bellow:
$tree= new DS_Tree();
$node=array('name' => 'start');
$tree->insert_node($node);
$tree->goto_root();
$mytree = new id3();
$mytree->init($data_array_AttrList,$data_array_values,$data_class,$data_array_instances,$tree);
$mytree->run();
echo '<pre class="brush: php">';
print_r($mytree->tree->draw_tree());
echo '</pre>';
The function draw_tree() is:
public function draw_tree() {
return $this->nodes;
}
The function that creates my tree is:
private function make_tree($attr) {
foreach($this->Values[$attr] as $v) {
$subset = $this->get_subset($attr, $v);
if($the_class = $this->has_same_class($subset)) {
$node =array(
'name' => $attr,
'arc' => $v
);
$this->tree->insert_node($node);
$this->Instance = array_diff_key($this->Instance, $subset);
} else {
$node =array(
'name' => $this->Classa,
'arc' => $v
);
$unresolved = $this->tree->insert_node($node);
}
}
if (isset($unresolved)) {
$this->tree->goto_index($unresolved);
}
}
}
The result is:
Array
(
[0] => Array
(
[name] => Time
[parent] =>
[children] => Array
(
[0] => 1
)
)
[1] => Array
(
[name] => Focus
[arc] => Array
(
[0] => 2 day/week
[1] => 3 day/week
[2] => 4 day/week
[3] => 5 day/week
[4] => 6 day/week
)
[parent] => 0
[children] => Array
(
[0] => 2
)
)
[2] => Array
(
[name] => Dificulty
[arc] => Array
(
[0] => Weght loss
[1] => Mantain weight
[2] => Gain Mass
)
[parent] => 1
[children] => Array
(
[0] => 3
)
)
[3] => Array
(
[name] => Sex
[arc] => Array
(
[0] => Beginner
[1] => Intermediar
[2] => Advance
)
[parent] => 2
[children] => Array
(
[0] => 4
)
)
[4] => Array
(
[name] => Array
(
[Exercise] => Array
(
[0] => Array
(
[0] => Ex1
[1] => Ex2
[2] => Ex3
[3] => Ex4
[4] => Ex5
)
)
)
[arc] => Array
(
[0] => F
[1] => M
)
[parent] => 3
)
)
Just to display an array as a tree:
echo "<pre>";
var_dump($array);
echo "</pre>";
Here is a way to iterate through this data structure and look for a certain value:
function recurseFind($tree, $find, $path = "") {
if (!is_array($tree)) {
// it is a leaf:
if ($tree === $find) {
return $path; // return path where we found it
}
return false;
}
foreach($tree as $key => $value) {
$result = recurseFind($value, $find, $path . (is_numeric($key) ? "[$key]" : "['$key']"));
if ($result !== false) return $result;
}
return false;
}
For the sample input you provided, if you would call it like this:
echo recurseFind($tree, "Mantain weight", '$tree');
Outputs the "location" of that value (first match only) in a format that can be evaluated in PHP:
$tree[2]['arc'][1]
I've this piece of code in which I want to know is there anyway I could avoid pass by reference
public function formatNumbers($numbersData){
$result = array();
array_map(
function($row) use (&$result) {
$result[$row['GroupId']][$row['Type']] = $row['value'];
}, $numbersData
);
return $result;
}
Input: $numbersData =
Array
(
[0] => Array
(
[GroupId] => 2
[Type] => 1
[value] => 82000
)
[1] => Array
(
[GroupId] => 2
[Type] => 3
[value] => 52000
)
[2] => Array
(
[GroupId] => 2
[Type] => 4
[value] => 30105
)
[3] => Array
(
[GroupId] => 2
[Type] => 7
[value] => 13266
)
)
Output is
Array
(
[2] => Array
(
[1] => 82000
[3] => 52000
[4] => 30105
[7] => 13266
)
)
I know I can do it using foreach, but I want to know that if there anyway to use array map for this without pass by reference.Any help would be greatly appreciated.
Wrong kind of operation. You're not looking for a mapping of values, you're looking for an array reduction:
return array_reduce($numbersData, function(array $acc, array $row) {
$acc[$row['GroupId']][$row['Type']] = $row['value'];
return $acc;
}, []);
You can do it using array_column() function.
$arr = array(array('GroupId'=>2,'Type' => 1,'value' => 82000),array('GroupId'=>2,'Type' => 3,'value' => 52000),array('GroupId'=>2,'Type' => 4,'value' => 30105),array('GroupId'=>2,'Type' => 7,'value' => 13266));
print_r(array_column($arr, 'value', 'Type'));
how can i count an element if it appears more than once in the same array?
I already tried with array_count_values, but it did not work, is it beacuse i got more than one key and value in my array?
This is my output from my array (restlist)
Array (
[0] => Array ( [restaurant_id] => 47523 [title] => cafe blabla)
[1] => Array ( [restaurant_id] => 32144 [title] => test5)
[2] => Array ( [restaurant_id] => 42154 [title] => blabla2 )
[3] => Array ( [restaurant_id] => 32144 [title] => test5)
[4] => Array ( [restaurant_id] => 42154 [title] => blabla2 )
)
I want it to count how many times the same element appears in my array and then add the counted value to my newly created 'key' called hits in the same array.
Array (
[0] => Array ( [restaurant_id] => 47523 [title] => cafe blabla [hits] => 1)
[1] => Array ( [restaurant_id] => 32144 [title] => test5 [hits] => 2)
[2] => Array ( [restaurant_id] => 42154 [title] => blabla2 [hits] => 2)
)
This is how i tried to do what i wanted.
foreach ($cooltransactions as $key)
{
$tempArrayOverRestaurants[]= $key['restaurant_id'];
}
$wordsRestaruants = array_count_values($tempArrayOverRestaurants);
arsort($wordsRestaruants);
foreach ($wordsRestaruants as $key1 => $value1)
{
$temprestaurantswithhits[] = array(
'restaurant_id' => $key1,
'hits' => $value1);
}
foreach ($restlistas $key)
{
foreach ($temprestaurantswithhits as $key1)
{
if($key['restaurant_id'] === $key1['restaurant_id'])
{
$nyspisestedsliste[] = array(
'restaurant_id' => $key['restaurant_id'],
'title' => $key['title'],
'hits' => $key1['hits']);
}
}
}
I know this is probably a noob way to do what i want but i am still new at php..I hope you can help
Just try with associative array:
$input = array( /* your input data*/ );
$output = array();
foreach ( $input as $item ) {
$id = $item['restaurant_id'];
if ( !isset($output[$id]) ) {
$output[$id] = $item;
$output[$id]['hits'] = 1;
} else {
$output[$id]['hits']++;
}
}
And if you want to reset keys, do:
$outputWithoutKeys = array_values($output);
I need some clarity as to what PHP function can achieve what I'm aiming for.
This is a PHP array I have:
Array
(
[0] => Array
(
[id] => 6
[index] => 1
[active] => 1
[name] => MyName
)
[1] => Array
(
[id] => 1
[index] => 2
[active] => 1
[name] => YourName
)
[2] => Array
(
[id] => 2
[index] => 4
[active] => 1
[name] => TheirName
)
}
I want to take the "index" value and make it a KEY of that array parent, so the array would become this:
Array
(
[1] => Array
(
[id] => 6
[index] => 1
[active] => 1
[name] => MyName
)
[2] => Array
(
[id] => 1
[index] => 2
[active] => 1
[name] => YourName
)
[4] => Array
(
[id] => 2
[index] => 4
[active] => 1
[name] => TheirName
)
}
Can anyone please tell me how would I do this in PHP?
Thank you in advance.
you can use: array_column($array, null, 'index');
is the better solution, but only work for >= 5.5 php version
Not the most elegant solution, but it works (it doesn't actually move the array, it just generates a new one that corresponds to your requirements):
$resultArr = array();
foreach ($mainArr as $value) {
$resultArr[$value['index']] = $value;
}
unset($mainArr); // or $mainArr = $resultArr;
This way you won't overwrite any existing keys in your original array.
$a = array
(
0 => array
(
"id" => 6,
"index" => 1,
"active" => 1,
"name" => "MyName"
),
1 => Array
(
"id" => 1,
"index" => 2,
"active" => 1,
"name" => "YourName"
),
2 => Array
(
"id" => 2,
"index" => 4,
"active" => 1,
"name" => "TheirName"
)
);
$newArray = array();
foreach ($a as $foo) {
$newArray[$foo['index']] = $foo;
}
You have to do it manually with:
$input = array( /* your data */ );
$output = array();
foreach ( $input as $values ) {
$output[ $values['index'] ] = $values;
}
public static function normArray($key, $inputArray)
{
$outputArray = array();
foreach ($inputArray as $item) {
$index = intval($item[$key]);
$outputArray[$index] = $item;
}
return $outputArray;
}