Make a multidimensional array of three array in PHP - php

I want to make a array of arrays.
Problem:
My final array will be like this:
Array(Array1, Array2, Array3);
and arrays will be
Array1=Array ( [0] => 0 [1] => 100 [2] => 100 [3] => 0 [4] => 0 [5] => 0 [6] => 0
Array2=Array ( [0] => 0 [1] => 100 [2] => 100 [3] => 60 [4] => 0 [5] => 30 [6] => 0
Array3=Array ( [0] => 50 [1] => 100 [2] => 100 [3] => 0 [4] => 0 [5] => 0 [6] => 40
So how can make the multidimensional array and how can I access data from this array. Help will be appreciated. Thanks

Unlike a statically typed language, there is no need to declare these up front which can be confusing to newcomers. Really what you're describing is just a two-dimensional array. So really you have two options. Assuming these aren't associative like your example above, either putting arrays together:
$array = array($array1, $array2, $array3);
Or if you are doing something with loops/iterators you can just define your two-d array on the fly:
$array[$itr][$inner_itr] = $array1[$inner_itr];
Hope that helps.

Related

How to Combine Array String As Keys And Number As Value In Php

I have a problem to do the combining between 2 arrays.
In the first array as a string (example array data):
Array
(
[0] => courses
[1] => courses
[2] => courses
[3] => courses
[4] => courses
)
Second array as a value (Example Array values):
Array
(
[0] => 64
[1] => 63
[2] => 62
[3] => 2
[4] => 9
)
After I tried to combine between 2 arrays like this:
$combine = (array_combine($data, $courses))
I can only get a combination result like this:
Array
(
[courses] => 9
)
How can i get the result of combination between the two arrays above, like this?
Array
(
[courses] => 64
[courses] => 63
[courses] => 62
[courses] => 2
[courses] => 9
)
Thanks In Advance.
You are not able to generate your intended output as you can't have multiple of the same key in an array. This is why you are ending up with one entry of 'courses'.
For example this would be do-able:
Array
(
[courses] => array(
[0] => 63
[1] => 63
[2] => 62
[3] => 2
[4] => 9
)
)
You need to change your approach.

Reduce memory size of my PHP array

I've just come across the problem of the large memory size of PHP arrays. I'm trying to store a fairly small amount of data per object, in a bunch of small arrays. The storage looks like this.
[Stats] => Array
(
[Apps] => Array
(
[Career] => Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
[4] => 0
[5] => 0
)
[Team] => Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
[4] => 0
[5] => 0
)
[Season] => Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
[4] => 0
[5] => 0
)
)
[SubApps] => Array
(
[Career] => Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
[4] => 0
[5] => 0
)
[Team] => Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
[4] => 0
[5] => 0
)
[Season] => Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
[4] => 0
[5] => 0
)
)
I need 20 or more of the middle arrays shown here (e.g 'Apps', 'SubApps' etc). It's an array of arrays of arrays, with the final arrays always being exactly 6 values long.
Since it's just 18 values, I'd assumed that it wasn't much memory, but I've found out that PHP uses about 144 bytes per value, so it's turning out to be huge, especially since I have about 3000 such objects. It's using about 30k per object, just on 20 of these array blocks.
So I'm looking for a memory efficient solution. Since my final array is always 6 values, I'm thinking that maybe I can pack/unpack. I also looked into SplFixedArray but it doesn't suit my needs in some ways and doesn't save much memory. I also want the values to show in a readable format in my database in case I need to edit them manually in the db (one method I tried saved memory but the values were garbled in the db).
UPDATE - Solution. I found a simple solution to this. The arrays are JSON_encode'd for saving to the database. When I load them, they're decoded. Initially I was decoding the whole batch at once, hence the massive memory usage, but the simple solution was to keep them encoded until needed, then decode - process - encode. This way, only one is ever decoded into the array and the rest remain encoded, which appears to use very small amounts of memory.

How to merge these two arrays in this specific order

I have two array as following
Array
(
[0] => 641
[1] => 622
[2] => 100
[3] => 6431
)
Array
(
[0] => 64.1
[1] => 62.2
[2] => 10
[3] => 643.1
)
How can I make it as following
Array
(
[0] => 641
[1] => 64.1
[2] => 622
[3] => 62.2
[4] => 100
[5] => 10
[6] => 6431
[7] => 643.1
)
It's as simple as
$result=array_merge($array1,$array2);
Note: Your values wont be in the order you presented though. If that is important then you need to loop through your arrays to build a new array accordingly.
Ummm ok here is that version as well
if(count($array1)==count($array2))
{
for($i=0;$i<count($array1);$i++)
{
$result[]=$array1[$i];
$result[]=$array2[$i];
}
}
print_r($result);
Fiddle
Manual
you can use array_merge() function merges one or more arrays into one array.
example:
array_merge(array1,array2,array3...)

PHP - count frequency of array values

Is there a way in php to count how often a value exists in a large array?
So if I have an array like this:
$array = "1,2,3,4,joe,1,2,3,joe,joe,4,5,1,6,7,8,9,joe";
is there a way to output a new array that tells me (and sorts) which is used most and how many for each?
$result = array(
[joe] => 4
[1] => 3
[2] =>2
etc...
)
I've seen the php array_count_values, but can this be sorted by most -> least? or is there an easier way?
Thanks everyone!
Sort them after counting them with arsort()
$result = array_count_values(explode(',', $array));
arsort($result);
Array
(
[joe] => 4
[1] => 3
[2] => 2
[4] => 2
[3] => 2
[9] => 1
[8] => 1
[5] => 1
[6] => 1
[7] => 1
)

Sorting Multidimensional Array by Specific Key

EDIT: For anyone who might come across this post with a similar problem, It was solved by taking konforce's supplied answer and tweaking around a bit with the custom sorting function:
function cmp($a, $b) {
if ($a[5] == $b[5]) {
return ($a[3] < $b[3]) ? -1 :1;
}
return ($a[5] > $b[5]) ? -1 : 1;
}
Notice $a[5] == $b[5] does not return zero. It was changed to check who has the most losses and then sort it in ASC order. I'm sure you can even keep going and add another if-statement in there in-case they have the same losses.
Lastly, all you do is usort($ARRAY, "cmp"); and finito!!!
Original Post
My apologies for coming up with yet another MD Array sorting question but I'm just not getting it. I've searched aplenty
for a solution and although many sites have provided what seemed like a logical answer I still have not been able to figure it out.
My problem is since I'm still learning its been rather difficult for me to grasp the concept of using usort with a custom comparing
function. Atleast, thats what I have seen the most when others have tried to sort MD Arrays.
I'm working on a small project to sharpen up on my php skills. Its a very basic tournament standings script that holds a team's information within an array. I would like to sort the array by most points($array[X][X][5]).
So the array looks something like this:
Array (
[0] => Array (
[0] => Array (
[0] => cooller
[1] => 6
[2] => 6
[3] => 0
[4] => 0
[5] => 18
)
)
[1] => Array (
[0] => Array (
[0] => strenx
[1] => 9
[2] => 5
[3] => 1
[4] => 3
[5] => 18
)
)
[2] => Array (
[0] => Array (
[0] => rapha
[1] => 10
[2] => 8
[3] => 1
[4] => 1
[5] => 25
)
) [3] => Array (
[0] => Array (
[0] => ronald reagan
[1] => 5
[2] => 4
[3] => 0
[4] => 1
[5] => 13
)
)
)
I would like to sort it by most points(cell #5), so it would look like this after sorting:
Array (
[0] => Array (
[0] => Array (
[0] => rapha
[1] => 10
[2] => 8
[3] => 1
[4] => 1
[5] => 25
)
)
[1] => Array (
[0] => Array (
[0] => cooller
[1] => 6
[2] => 6
[3] => 0
[4] => 0
[5] => 18
)
)
[2] => Array (
[0] => Array (
[0] => strenx
[1] => 9
[2] => 5
[3] => 1
[4] => 3
[5] => 18
)
)
[3] => Array (
[0] => Array (
[0] => ronald reagan
[1] => 5
[2] => 4
[3] => 0
[4] => 1
[5] => 13
)
)
)
The player with 25 points would be at the top, followed by 18, 18, and lastly 13. Sorry for my earlier post, was having difficulty wording my question correctly. Thanks in advanced!
I think you want something like this:
usort($standings, function($a, $b) { return $b[0][5] - $a[0][5]; });
Or prior to PHP 5.3:
function cmp($a, $b) { return $b[0][5] - $a[0][5]; }
usort($standings, 'cmp');
When using usort, the $a and $b parameters will be one "layer" into the supplied array. So in your case, an example of $a or $b will be:
[0] => Array (
[0] => cooller
[1] => 6
[2] => 6
[3] => 0
[4] => 0
[5] => 18
)
I'm not sure why you have an extra containing array there, but as you can see, you want to sort based on the [0][5] position.
usort($standings[0][0][5], 'cmp') won't work because the first parameter isn't an array to sort, it's just a single number, the points.

Categories