PHP Arrays - getting multi-dimensional values - php

I have the following Array data:
array(1) {
[0]=> array(8) {
[0]=> string(2) "55"
[1]=> string(1) "2"
[2]=> string(1) "1"
[3]=> string(1) "3"
[4]=> string(1) "4"
[5]=> string(1) "5"
[6]=> string(1) "6"
[7]=> string(1) "7"
}
}
I'd like to be able to bring back the values, individually for [1] and [2] for examples, who's values are 2 and 1 respectively.

$matches[0][1] - cannot believe I actually asked this question.

In a multidimensional array, you need to reference the nested array, and then the nested array element.
For example, if your array was set in $array, you would reference it as:
$array[0][1];
Which is basically saying 'Value of 2nd element of 1st nested array'.

Related

php array_diff wrong reuslt

Hello i have an array_dif function between 2 arrays and the result it's not as it should.I don't understand why it does not return the status as difference. First array is data second is row and the third is the result. In the result it should also be status because the value is different.
$result = array_diff($data,$row );
array(9) {
["scooter_id"]=>
string(6) "RO0001"
["battery_lvl"]=>
string(2) "80"
["lat"]=>
string(9) "44.312150"
["lng"]=>
string(9) "23.872900"
["alt"]=>
string(1) "0"
["speed"]=>
string(1) "0"
["status"]=>
string(1) "2"
["ip"]=>
string(14) "213.233.101.62"
["port"]=>
int(24600)
}
array(11) {
["battery_lvl"]=>
string(2) "80"
["nr_satelites"]=>
string(1) "1"
["lat"]=>
string(9) "44.312154"
["longi"]=>
string(9) "23.873007"
["alt"]=>
string(1) "0"
["speed"]=>
string(1) "0"
["status"]=>
string(1) "1"
["location"]=>
string(7) "romania"
["ip"]=>
string(14) "213.233.101.62"
["port"]=>
string(5) "24600"
["status_intermediar"]=>
string(1) "2"
}
array(3) {
["scooter_id"]=>
string(6) "RO0001"
["lat"]=>
string(9) "44.312150"
["lng"]=>
string(9) "23.872900"
}
array_diff checks only the values.
Because your 2nd array contains ["status_intermediar"]=> string(1) "2" it finds the value so it doesn't see it as a difference
If you want to check both keys and values you should use array_diff_assoc
Also if you want to find all the different values from BOTH arrays you should run it twice
$difference1=array_diff_assoc($array1,$array2);
$difference2=array_diff_assoc($array2,$array1);
array_dif is one way function ("Compares array1 against one or more other arrays and returns the values in array1 that are not present in any of the other arrays."- https://www.php.net/manual/en/function.array-diff.php).
If you want all diffs, you have to call it twice: array_dif($first, $second) and array_dif($second, $one) and optionally merge results.
$array_difference1 = array_merge(array_diff($array1, $array2),
array_diff($array2, $array1));
$array_differnce = array_merge(array_diff($array_difference1, $array3),
array_diff($array3, $array_difference1));

Count values in specific position in multidimensional array

I have a multidimensional array that looks like this:
array(6) {
[0]=>
array(6) {
[0]=>
string(5) "email"
[1]=>
string(5) "vote1"
[2]=>
string(5) "vote2"
[3]=>
string(5) "vote3"
[4]=>
string(5) "vote4"
[5]=>
string(5) "vote5"
}
[1]=>
array(6) {
[0]=>
string(5) "a#a.a"
[1]=>
string(1) "A"
[2]=>
string(1) "B"
[3]=>
string(1) "C"
[4]=>
string(1) "D"
[5]=>
string(1) "E"
}
[2]=>
array(6) {
[0]=>
string(5) "b#b.b"
[1]=>
string(1) "A"
[2]=>
string(1) "B"
[3]=>
string(1) "C"
[4]=>
string(1) "D"
[5]=>
string(1) "E"
}
[3]=>
array(6) {
[0]=>
string(5) "c#c.c"
[1]=>
string(1) "A"
[2]=>
string(1) "B"
[3]=>
string(1) "C"
[4]=>
string(1) "D"
[5]=>
string(1) "E"
}
[4]=>
array(6) {
[0]=>
string(5) "d#d.d"
[1]=>
string(1) "A"
[2]=>
string(1) "B"
[3]=>
string(1) "C"
[4]=>
string(1) "D"
[5]=>
string(1) "E"
}
[5]=>
array(6) {
[0]=>
string(5) "e#e.e"
[1]=>
string(1) "A"
[2]=>
string(1) "B"
[3]=>
string(1) "C"
[4]=>
string(1) "D"
[5]=>
string(1) "E"
}
}
I want to count how many times each value occurs at position [1] of each array.
I have tried many things including array_count_values(), but this doesn't allow the option to count occurrences in just a certain position of each array. I'm not quite sure how to go about this issue because the array is multidimensional and I have never had to deal with just counting the values at a specific position.
I apologize if the answer to this is blatantly obvious, I've been working on this issue for a few hours but to no avail, but I also am a beginner when it comes to arrays, especially multidimensional. All ideas are welcome :)
What you can do is, you can loop through the array and put the values of [1] into another array, and apply array_count_values on the resultant array.
$a = array();
foreach ($arr as $ar) {
$a[] = $ar[1];
}
print_r(array_count_values($a));
This will give you:
vote1: 1
A: 5
If the above is what you are looking for? If you want a shorter version, you can also use array_column.
use array_column($array, 1) to get position[1] elements as array; Then use your array_count_values() to count them.
<?php
$array = [[1,2,3],[2,3,4]];
var_dump(array_count_values ( array_column($array,1) ));

PHP array_combine() function not working properly?

I'm trying to combine two array in PHP with array_combine() function, but sometimes it working fine and sometimes it's not. I can't understand why it's working like this!
My Code:
var_dump($selectedDuretion);
var_dump($selectedDuretionType);
$combination = array_combine($selectedDuretion, $selectedDuretionType);
return $combination;
Expected OUTPUT:
array(4)
{
[0]=> string(1) "3"
[1]=> string(2) "12"
[2]=> string(1) "4"
[3]=> string(1) "3"
}
array(4)
{
[0]=> string(4) "days"
[1]=> string(4) "days"
[2]=> string(5) "weeks"
[3]=> string(5) "weeks"
}
{"3":"days","12":"days","3":"weeks","4":"weeks"}
Actual OUTPUT :
array(4)
{
[0]=> string(1) "3"
[1]=> string(2) "12"
[2]=> string(1) "4"
[3]=> string(1) "3"
}
array(4)
{
[0]=> string(4) "days"
[1]=> string(4) "days"
[2]=> string(5) "weeks"
[3]=> string(5) "weeks"
}
{"3":"weeks","12":"days","4":"weeks"}
The combination of arrays it shocking, I'll be thankful if anyone tell me why is this happening and how to solve it.
PHP Does not allow you to have duplicate indices in an array while JSON does allow you to have that for whatever reasons.
Since you are trying to convert PHP arrays to JSON your duplicate key gets eliminated. Hence you will have to manually build the JSON string.
$json="";
for($i=0;$i<count($selectedDuration);$i++)
{
$json.='"'.$selectedDuration[$i].'":"'.$selectedDurationType[$i].'",';
}
$json=rtrim($json,",");
$json="{".$json."}";
echo $json;
Output
{"3":"days","12":"days","4":"weeks","3":"weeks"}
Fiddle

Split large array into several arrays based on array field value

I have an array, and its large. very large. Its for a real estate company. I would like to split the array into sections based on the number of bedrooms each home has. So ideally all the entries (like the one below) that had 0 bedrooms would be set into one array, and with 1, into another. etc...
Is this possible?
object(SimpleXMLElement)#124 (1) {
["unit"]=>
array(40) {
[0]=>
object(SimpleXMLElement)#246 (1) {
["#attributes"]=> array(28) {
["mlsnumber"]=> string(8) "A1837040"
["type"]=> string(0) ""
["unit"]=> string(3) "607"
["maintenancefee"]=> string(4) "1609"
["price"]=> string(6) "890000"
["salesprice"]=> string(0) ""
["bath"]=> string(1) "1"
["hbath"]=> string(1) "0"
["beds"]=> string(1) "0"
["sqft"]=> string(3) "747"
["sqftm"]=> string(5) "69.40"
["pricesqft"]=> string(8) "1,191.43"
["lat"]=> string(16) "25.7683201213024"
["lng"]=> string(16) "-80.132474899292"
["pricemeters"]=> string(9) "12,824.21"
["pricechange"]=> string(5) "-6.32"
["dom"]=> string(3) "181"
["reo"]=> string(1) "N"
["shortsale"]=> string(1) "N"
["dropprice"]=> string(0) ""
["pets"]=> string(3) "Yes"
["furnished"]=> string(1) "U"
["FloorLevel"]=> string(1) "6"
}
}
Loop through the array and push the current array into a new array whereas the index of the new array is the number of bedrooms of the current array. You'll need to convert your object into an array first, then something along the lines of this:
foreach($all_units as $unit){
if(!isset($new[$unit['beds']])){
$new[$unit['beds']] = array();
}
array_push($new[$unit['beds']],$unit);
}
print_r($new);
Suppose $arr holds your result, Try
$result = array();
foreach($arr['unit'] as $prop){
$result[$prop["beds"]][] = $prop;
}

Insert element into array [duplicate]

This question already has answers here:
How to insert element into arrays at specific position?
(25 answers)
Insert new item in array on any position in PHP
(23 answers)
Closed 9 years ago.
I have an array
array(1) {
[0]=>
array(4) {
[0]=>
string(1) "1"
[1]=>
string(2) "10"
[2]=>
string(3) "100"
[3]=>
string(3) "200"
}
}
I want to insert two element into the array which must be the 3rd and last element.
Output:
array(6) {
[0]=>
array(6) {
[0]=>
string(1) "1"
[1]=>
string(2) "10"
[2]=>
string(1) ""
[3]=>
string(3) "100"
[4]=>
string(3) "200"
[5]=>
string(1) ""
}
}
how can i do this?
What I have tried
array_splice($input,3 ,0,"");
Then result become like this, the array not added in the middle
array(6) {
[0]=>
array(6) {
[0]=>
string(1) "1"
[1]=>
string(2) "10"
[2]=>
string(1) ""
[3]=>
string(3) "100"
[4]=>
string(3) "200"
[5]=>
string(1) ""
}
[1]=>
array(1) {
[0]=>
string(1) ""
}
}
To insert in the middle of array, you can use array_splice with a length of 0.
array_splice($input, 3, 0, "");
To add to the array, you can use either array_push or [] operator
By using array_splice you can insert element inside the array
$array = [0 => 'Data', 1 => 'data2', 2=> 'data3'];
array_splice($array, 1, 0, 'data append');
var_dump($array);

Categories