How to find value inside multidimensional array without looping? - php

I have a multidimensional array stored in $accounts variable :
Array
(
[0] => Array
(
[id] => 1
[account] => ACR016
[desc] => Salary
)
[1] => Array
(
[id] => 2
[account] => ACR017
[desc] => Bonuses
)
)
I'd like to find/get the "desc" value by using its "account" inside $accounts,
In SQL i will go like this :
SELECT desc FROM table WHERE account = 'ACR016';
How to do that with PHP without do the SQL query (because the array already stored in variable) and without looping ?

Use https://www.php.net/manual/en/function.array-column.php for this.
As this is a builtin function it should perform better than doing your own loop.
Example:
$accountDescriptions = array_column($accounts,'desc','account');
Will result in:
Array
(
[ACR016] => Salary
[ACR017] => Bonuses
)
Alternatively you can use:
Example:
$accountDescriptions = array_column($accounts,null,'account');
Then you will get all data keyed with account instead of numeric index.

Related

Convert large array of array to associative array without loop

I am using aws redis cache for quicker results instead of saving in db.
With this method
$result = $client->listTagsForResource([
'ResourceName' => '<string>', // REQUIRED
]);
Now it gives me result in given format.
Array
(
[0] => Array
(
[Key] => key1
[Value] => string1
)
[1] => Array
(
[Key] => status
[Value] => 1
)
)
I am unable to find a function in amazon docs which can give me direct results, so I decided to search in array , but finding in very large array with loops cost me in terms of time. So is there a way to convert it in following
Array
(
[key1] => string1,
[status] => 1
)
So I can directly access array index by using $array['key1']
You can try something like this to create new array:
$newArray = array_combine(
array_column($array, 'Key'),
array_column($array, 'Value')
);
echo $newArray['status'];

Turning associative JSON array into nested SQL WHERE clause

I'm new to backend programming and CodeIgniter is my first framework. This is new to me. Right now, the thing that I'm working is that I need to create a query based on the JSON encoded data saved at my table.
I have here a segmentation form that should build a query to look/search for the customers profiles based on the condition set
Segmentation form
$segment = array(
...
'filters' => json_encode($post['filters']), //to insert the filters in json
...
);
if ($this->model->addSegments($segment))
The filters saved in the database are like this :
{"data":[{"data":{"condition":"0","attribute":"gender","sign":"=","value":"Male"},"type":"condition","operator":"0","filter":{"operator":"0"}}],"type":"match","operator":"0"}
Looking for the answers, I found this forum and I relate it to the problem that I had: http://www.codingforums.com/php/202207-turning-json-object-into-nested-sql-where-clause.html
Calling the same like function (solution at the link) to the controller using this lines:
$filter = $this->model->getSegmentsFilters($id_segment);
$filters = $this->model->parseFilterToQuery(json_decode($filter['filters'], true));
With the parameter TRUE from the json_decode, It throws the filters in array and using print_r shown like this:
Array (
[0] => Array (
[data] => Array (
[condition] => 0
[attribute] => firstname
[sign] => =
[value] => John )
[type] => condition
[operator] => 0
[filter] => Array (
[operator] => 0 )
)
[1] => Array (
[data] => Array (
[condition] => 0
[attribute] => gender
[sign] => =
[value] => Male )
[type] => condition
[operator] => condition
[filter] => Array (
[operator] => 0 )
)
)
the $sql inside if(is_array) doesn't show any value to concatenate with. I want it to concatenate with another query as a WHERE clause like this idea.
$sql = "SELECT * FROM customers WHERE" . parseFilterToQuery($filters);
If you think that the solution/procedure that I came up is correct. My question is that inside the if(is_array) condition,
how should I work on it with associative JSON array?

Can you retrieve nested array by name in php

I have an array like this:
(
[data] => Array
(
[account_id] => 1
[description] => my asset
[value] => Estimate
[value_amount] => 85000
[type] => Vehicle
[owner] => Array
(
[app_id] => 123
[percent] => 100
)
)
)
Clearly I can loop through the array and pull out the nested owner array that way, but is there something similar to array_column that will get the entire owner nested array without having to loop ?
Use the indexes, no function necessary.
$owner = $array['data']['owner']
or..
$percent = $array['data']['owner']['percent']
In php it's call associative array which mean index will not numeric it can be anythink.
So you can retrieve data like
<?php echo $array['data']['owner']['app_id']; ?>

Is it possible to insert the record using multi dimension array?

The scenario is
I would like to insert an record like this:
Firstly, i have an array of what datafield i need to insert
$field = Array ( [0] => Email [1] => Name )
Secondly, I have an array of mail
$mail = Array ( [0] => a#a.com [1] => foodil#g.com )
Lastly, I have an multi dimension array that has Name,
$set = Array ( [1] => Array ( [1] => leo [4] => NULL ) )
however, It can be more than one field,
eg. it can be also have a field of phone (and also address, geneder...whatever) , then it will be:
$field = Array ( [0] => Email [1] => Name [2] => Phone )
$set = Array ( [1] => Array ( [1] => leo [4] => NULL ) [5] => Array ( [1] => 4343343 [4] => 3453343 ))
The problem is , how to insert in the scenario like this? :
The query should look like this
$query="INSERT INTO subscriber (Email,Name,Phone) VALUES ($mail[] , $set[][], $set[][])";
why you not simply insert a single record for each subscriber?, if the fields on the databases are NOT-NULL you can insert default values instead. however, i would use json_encode() instead serialized(), this will maintain the data readble for others languages using a json parser.
You can use serialize() and unserialize (its opposite) to store multidimensional arrays into mysql

Return values by Yii db

I have a following return value from Yii framework query, using the methods and classes built in Yii:
Array
(
[0] => Array
(
[id] => 1
[title] => Developer
)
[1] => Array
(
[id] => 2
[title] => Tester
)
)
I would like to (using nothing but Yii), rearange this like:
Array
(
[1] => Developer
[2] => Tester
)
Meaning, I will not group the results in a specific index of the array, and I will enlist them all in one array, AND my key/indexes will represent the value of ID field from my table, and value of those keys will be values from my "role" field from my table.
Is this possible and how?
You can use the following method, defined here:
CHtml::listData($yourArray, 'id', 'title');

Categories