How to access multi array value directly? - php

I have an array like array ( array 0 ('item1'=>1,'item2'=>3))....etc like so.
And I want to access say the value of item2 but I don't want to use array[0]['item2']...Is there another way to access it? I just need 1 value so I don't think putting it in a foreach loop would be efficient..
Any ideas?

You have two choices: if you know the row and column you want to access, then you can access it directly. If you don't know the row and column, then you'd need to use a foreach loop to find the item you want.
You can only access it directly if you know where it is.

Assuming you meant array ( 0 => array ('item1'=>1,'item2'=>3)), you can use
array_values() to renumber the top level values. Then you know that the first value ('item1'=>1,'item2'=>3) will be indexed by the key 0.

Related

Sum of Column in PHP

I am trying to get the sum of a column using PHP.
However this is my output:
Array ( [0] => Array ( [OrderPrice] => 5.99 ) )
Price: 0
Can someone please explain why I am not getting the sum, which should be 5.99 as shown above.
This is not a direct answer/solution to your immediate PHP problem, which some guru might address, but if you want to sum a SQL column, you would probably be better off doing this in SQL:
SELECT SUM(OrderPrice) AS order_price_sum FROM PizzaOrder
As you have an array of arrays, you need to sum up the OrderPrice elements of each sub array. So in PHP you would extract this column using array_column() and add this array instead...
echo array_sum(array_column($orderArr, 'OrderPrice'));
BUT, you should do this in MySQL, unless you also need the data for other purposes.
why you just edit your sql statement to get the sum
SELECT SUM(OrderPrice) FROM PizzaOrder
if you need the all values SELECT OrderPrice,SUM(OrderPrice) FROM PizzaOrder
and you can get the sum from the whole column.

Remove duplicate values from PHP array preferring text keys

When I retrieve the results from a database query in PHP, I receive duplicated values, where ones is an integer, and one has the column name. array_unique(), in sorting the array, would in most cases end up with the numeric key before the string, meaning that would be the key kept. Right now, I use a function that removes from the array anything with a numeric key, but I don't really care for this approach. Does anyone have a better way to do this?
Change the command you use to retrieve the values from the database (eg. mysql_fetch_assoc instead of mysql_fetch_array). No matter which api you use now, there is an alternative that does exactly what you want.
update:
In PDO you would write:
$nonumindexes = $res->fetch(PDO::FETCH_ASSOC);
I suspect you're using mysqli_fetch_array() to retrieve the results?
If so, the 2nd parameter allows you to retrieves results as an associative array, numeric array or both. Or you can simply use mysqli_fetch_row or mysqli_fetch_assoc to get the results in the format you want.
http://www.php.net/manual/en/mysqli-result.fetch-array.php

Simple way to identify repeated strings in an array? (PHP)

I have an array with values $somearray = array('car','bike','legs,'car'). I'd like to find out which of these values in $somearray are repeated and pick out the index. In this example the answer would be 'car' and the index of the array would be 0 and 3.
I'm wondering if this can be done simply in a few lines, perhaps using some PHP function that I don't know, or do I need to explicitly make comparison in nested loops?
TIA!
The solution is pretty simple and I bet you can write it yourself. All you need is just 2 functions: array_count_values() and array_keys() with specified second argument (thanks to #prodigitalson)
There's a good answer on this similar question - How to detect duplicate values in PHP array?
To get the array indexes, I would then filter any array value > 1 and get the indexes

fastest way to get parent array key in multidimensional arrays with php

what is the best way to get parent array key with multidimensional arrays?
for example I have this array:
array(
[0] => array(0=> sample, 1=>picture, 2=>frame, 3=>google)
[1] => array(0=> iphone, 1=>orange, 2=>love, 3=>msn)
[2] => array(0=> joe, 1=>geee, 2=>panda, 3=>yahoo)
)
now I need to search for example google and get the parent array key..
which it should be 0...any ideas? I used for loop for this but I think it will be slow if I have arrays with 700000 rows..
If you have an array with 700,000 rows you are almost certainly doing something wrong... I would first recomend thinking about utilizing a different data store: flat file or some type of DB.
foreach($array as $key => $value) {
if(in_array('google', $value)) return $key
}
Arrays with 700,000 rows? How many arrays? 9/10 times problem is that you've got your data set up wrongly.
I'm going to go ahead and assume you're doing a search of some sort. As you can't index an array (in the search meaning of index) then you're probably best putting the data into a database and making the most of column indexing to search fast.
Depending on context, you may alternatively want to think about storing your data in files, one per array, and using file searches to find which file contains your value.

displaying array in tree format

Its a simple problem but I think i am stuck..I need to get the array's elements to be displayed in a tree format in php.. the array elements will be fetched from the database..
Recursively iterate over the array, test for arrayness with is_array(), other wise print out. Modify for whether you want preorder, inorder, or postorder. Done.

Categories