Insert element into array [duplicate] - php

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);

Related

PHP - get main array key search by value into two-level multidimensional array [duplicate]

This question already has answers here:
PHP multidimensional array search by value
(23 answers)
Closed 1 year ago.
I have an $array with build:
array(5) {
[0]=>
array(5) {
["woj"]=>
string(4) "2"
["pow"]=>
string(2) "11"
["gmi"]=>
string(1) "2"
["rodz_gmi"]=>
string(1) "2"
["name"]=>
string(10) "Test1"
}
[1]=>
array(5) {
["woj"]=>
string(1) "2"
["pow"]=>
string(2) "11"
["gmi"]=>
string(1) "2"
["rodz_gmi"]=>
string(1) "2"
["name"]=>
string(8) "test2"
}
[2]=>
array(5) {
["woj"]=>
string(1) "2"
["pow"]=>
string(2) "11"
["gmi"]=>
string(1) "2"
["rodz_gmi"]=>
string(1) "2"
["name"]=>
string(12) "test3"
}
[3]=>
array(5) {
["woj"]=>
string(1) "2"
["pow"]=>
string(2) "11"
["gmi"]=>
string(1) "2"
["rodz_gmi"]=>
string(1) "2"
["name"]=>
string(8) "Test4"
}
[4]=>
array(5) {
["woj"]=>
string(1) "2"
["pow"]=>
string(2) "11"
["gmi"]=>
string(1) "2"
["rodz_gmi"]=>
string(1) "2"
["name"]=>
string(11) "Test5"
}
}
and I want to create a php function, which can be search main array key by value (name).
Example - search main array key by name: test3. Result should be 2, because second main key of $array contain subarray where name key = test3.
I tied with array_search, but with multidimensional array, this function not work.
Thanks for your help.
You may try the following for your multi-dimensional array:
function myCustomSearch($data,$searchVal){
foreach($data as $index => $subArray){
foreach($subArray as $key=>$value){
if($value === $searchVal)return $index;
}
}
return null; //return null if not found
}
eg use assuming your current array shared in question is stored in $data
$resultIndex = myCustomSearch($data,'test3');
Let me know if this works for you.

I have 2d array, and i want to swap the value of the last index of each array

i want the last index value of each array for example 8 and 10 will be swap
array(2) {
[0]=>
array(5) {
[0]=>
string(1) "1"
[1]=>
string(1) "3"
[2]=>
string(1) "5"
[3]=>
string(1) "8"
}
[1]=>
array(5) {
[0]=>
string(1) "1"
[1]=>
string(1) "3"
[2]=>
string(1) "6"
[3]=>
string(2) "10"
}
}
so the result will be
array(2) {
[0]=>
array(5) {
[0]=>
string(1) "1"
[1]=>
string(1) "3"
[2]=>
string(1) "5"
[3]=>
string(2) "10"
}
[1]=>
array(5) {
[0]=>
string(1) "1"
[1]=>
string(1) "3"
[2]=>
string(1) "6"
[3]=>
string(1) "8"
}
}
if there are two value or more (even, not odd) will be swap its okay, as long as not all of the value will be swap. the size of array will be dynamic, thats just for example.
it swaps the last value:
$arr1 = array(1, 3, 5, 8);
$arr2 = array(1, 3, 6, 10);
$val1 = end($arr1);
$val2 = end($arr2);
array_pop($arr1);
array_pop($arr2);
array_push($arr1, $val2);
array_push($arr2, $val1);
print_r($arr1);
print_r($arr2);

Creating an array reading from different files in PHP

I'm trying the following,
I have different files (8 files) that stores values like this:
1 2 3 4 5 6
7 8 9 10 11 12
....................
I would like to read 8 files at the same time and create an array that will store the first value of each file, another array with the second element, and so on.
For example, if first element of file1 is 1, in file2 8, ..., in file8 23; the resulting array in this iteration would be:
first_array = [1, 8, ....., 23]
I was making some tests on reading files in PHP like this:
$myfile = fopen("textoPrueba.txt", "r",
"/home/berni/Documentos/Vesta1");
// Output one character until end-of-file
while(!feof($myfile))
{
echo fgetc($myfile);
}
fclose($myfile);
This code just show me the elements of a file, but I would like to take specific elements in an iteration.
Someone can give me a hint? Thanks in advance
(NOTE: files have more than a million of elements)
Another option is that, after we would file_get_content or read our files, we would use a simple expression and collect our numbers using a preg_match_all:
$re = '/([0-9]+)/m';
$str = '1 2 3 4 5 6
7 8 9 10 11 12
1 2 3 4 5 6
1 2 3 4 5 6
';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
foreach ($matches as $key => $numbers) {
foreach ($numbers as $key2 => $number) {
echo $number . "\n";
}
}
var_dump($matches);
array(24) {
[0]=>
array(2) {
[0]=>
string(1) "1"
[1]=>
string(1) "1"
}
[1]=>
array(2) {
[0]=>
string(1) "2"
[1]=>
string(1) "2"
}
[2]=>
array(2) {
[0]=>
string(1) "3"
[1]=>
string(1) "3"
}
[3]=>
array(2) {
[0]=>
string(1) "4"
[1]=>
string(1) "4"
}
[4]=>
array(2) {
[0]=>
string(1) "5"
[1]=>
string(1) "5"
}
[5]=>
array(2) {
[0]=>
string(1) "6"
[1]=>
string(1) "6"
}
[6]=>
array(2) {
[0]=>
string(1) "7"
[1]=>
string(1) "7"
}
[7]=>
array(2) {
[0]=>
string(1) "8"
[1]=>
string(1) "8"
}
[8]=>
array(2) {
[0]=>
string(1) "9"
[1]=>
string(1) "9"
}
[9]=>
array(2) {
[0]=>
string(2) "10"
[1]=>
string(2) "10"
}
[10]=>
array(2) {
[0]=>
string(2) "11"
[1]=>
string(2) "11"
}
[11]=>
array(2) {
[0]=>
string(2) "12"
[1]=>
string(2) "12"
}
[12]=>
array(2) {
[0]=>
string(1) "1"
[1]=>
string(1) "1"
}
[13]=>
array(2) {
[0]=>
string(1) "2"
[1]=>
string(1) "2"
}
[14]=>
array(2) {
[0]=>
string(1) "3"
[1]=>
string(1) "3"
}
[15]=>
array(2) {
[0]=>
string(1) "4"
[1]=>
string(1) "4"
}
[16]=>
array(2) {
[0]=>
string(1) "5"
[1]=>
string(1) "5"
}
[17]=>
array(2) {
[0]=>
string(1) "6"
[1]=>
string(1) "6"
}
[18]=>
array(2) {
[0]=>
string(1) "1"
[1]=>
string(1) "1"
}
[19]=>
array(2) {
[0]=>
string(1) "2"
[1]=>
string(1) "2"
}
[20]=>
array(2) {
[0]=>
string(1) "3"
[1]=>
string(1) "3"
}
[21]=>
array(2) {
[0]=>
string(1) "4"
[1]=>
string(1) "4"
}
[22]=>
array(2) {
[0]=>
string(1) "5"
[1]=>
string(1) "5"
}
[23]=>
array(2) {
[0]=>
string(1) "6"
[1]=>
string(1) "6"
}
}
Demo
Making the following assumptions:
each file has a string of numbers separated by spaces
numbers are not bigger than a billion
code to extract the first element of each file, starting with the code you've provided:
$myfile = fopen("textoPrueba.txt", "r", "/home/berni/Documentos/Vesta1");
// get the first 10 elements one at a time
$str = array();
for($i=0; $i<10; $i++) {
// get the first 10 elements one at a time
$str[] = fgetc($myfile);
}
fclose($myfile);
// squish them into a single string
$temp = join($str);
// explode it into an array separated by spaces
$split = explode(' ', $temp);
// get the first number
$first_element_of_file = $split[0];
For learning purposes, this will do what you asked. I make no claims about it being the best way to approach it!

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

Categories