I'm not sure if there is some kind of php function that will help me do this fairly simply or not. I figured I'd ask.
Let's say I have 5 products [prod1, prod2, prod3, prod4, prod5]
All of these products are related to eachother, so I need to arrive at something like this:
prod1, prod2, prod3, prod4, prod5
prod2, prod3, prod4, prod5, prod1
prod3, prod4, prod5, prod1, prod2
prod4, prod5, prod1, prod2, prod3
prod5, prod1, prod2, prod3, prod4
echo, save as variables, it doesn't matter to me.
In my example I said 5, but in reality there could be any number of products. Is there a function that does this automatically up to n products?? I don't even know what to really call this other then I'm matching them together.
You can do this:
$arr = array($prod1, $prod2, $prod3, $prod4, $prod5);
for ($i = 0; $i < count($arr); $i++) {
array_push($arr, array_shift($arr));
print_r($arr);
}
Another solution:
$array = array('$prod1', '$prod2', '$prod3', '$prod4', '$prod5');
$result = array();
for ($i = 0; $i < count($array); $i++) {
$result[] = array_merge( array_slice($array, $i), array_slice($array, 0, $i) );
}
print_r($result);
Output:
Array
(
[0] => Array
(
[0] => $prod1
[1] => $prod2
[2] => $prod3
[3] => $prod4
[4] => $prod5
)
[1] => Array
(
[0] => $prod2
[1] => $prod3
[2] => $prod4
[3] => $prod5
[4] => $prod1
)
[2] => Array
(
[0] => $prod3
[1] => $prod4
[2] => $prod5
[3] => $prod1
[4] => $prod2
)
[3] => Array
(
[0] => $prod4
[1] => $prod5
[2] => $prod1
[3] => $prod2
[4] => $prod3
)
[4] => Array
(
[0] => $prod5
[1] => $prod1
[2] => $prod2
[3] => $prod3
[4] => $prod4
)
)
Not sure I understood what you want exactly, but considering that your original data is a string, you can simply call 'explode' (http://php.net/manual/en/function.explode.php) or similar to turn it into an array.
Try this:
$products = array();
$line= array("prod1", "prod2", "prod3", "prod4", "prod5");
array_push($products , $line);
print_r($products);
Related
I want to merge those three arrays:
array (
[0] => zooey,
[1] => Robert,
[2] => james,
[3] => Alfred,
);
array (
[0] => city1,
[1] => city2,
[2] => city3,
[3] => city4,
);
array (
[1] => city1,
[2] => city2,
[3] => city3,
[4] => city4,
);
into this:
array (
[0] => array('id'=>user_id1,'name'=>username1, 'city'=>user city1),
[1] => array('id'=>user_id2,'name'=>username2, 'city'=>user city2),
// and so on
);
You need to write your own merge function ,if all 3 arrays are same size with correct positioning of relevant elements, the function would look like this
function combineUserArray($arrayNames, $arrayCities, $arrayIds){
$users = [];
for($i = 0; $i < count($arrayIds); $i++){
$users[$i] = [
'id'=>$arrayIds[$i],
'name'=>$arrayNames[$i],
'city'=>$arrayCities[$i],
];
}
return $users;
}
i need a function that transforms a 2d array from:
1 4
2 5
3 6
to
1 2
3 4
5 6
my first approach was by transforming the matrix with
array_unshift($data, null);
call_user_func_array('array_map', $data);
But now i got:
1 5
2 6
3
4
Can someone help?
Edit:
Ok let me be more clear i have these categories and i need to reorder them in this way
Array
(
[0] => Array
(
[0] => Das ist los
[1] => Land & Leute
[2] => Wirtschaft & Politik
[3] => Leben
[4] => Kultur
)
[1] => Array
(
[0] => Chronik
[1] => Motor
[2] => Sport
[3] => Blaulicht
[4] => Schauplatz
)
)
I need:
Array
(
[0] => Array
(
[0] => Das ist los
[1] => Wirtschaft & Politik
[2] => Kultur
[3] => Motor
[4] => Blaulicht
)
[1] => Array
(
[0] => Land & Leute
[1] => Leben
[2] => Chronik
[3] => Sport
[4] => Schauplatz
)
)
For a simple 2-column array:
$myArray = [
[1,4],
[2,5],
[3,6],
];
$myNewArray = array_chunk(
array_merge(
array_column($myArray, 0),
array_column($myArray, 1)
),
2
);
var_dump($myNewArray);
EDIT
For a more generic solution:
$myArray = [
[1,6,11],
[2,7,12],
[3,8,13],
[4,9,14],
[5,10,15],
];
$columns = count($myArray[0]);
$tmpArray = [];
for($i = 0; $i < $columns; ++$i) {
$tmpArray = array_merge(
$tmpArray,
array_column($myArray, $i)
);
}
$myNewArray = array_chunk(
$tmpArray,
$columns
);
var_dump($myNewArray);
Hello I'm trying to solve the following issue. I have some code which is self explanatory but I need to add a couple of lines to it
I would like filter the lower value arrays defined by the key value (in this case [2]) via the key value [1]. So if I have 3 arrays which contain key [1] with a value 100, then the arrays should be filtered via key [2].
Example of my code so far:
foreach($data as $line) {
if(substr($line,0,1)=="A") {
if(!$first) {
$parts = explode(chr(9), $line);
list($num1, $num2) = explode('_', $parts[1]); //code comes first / tested and works
$parts[2] = isset($num2) ? $num2 : $parts[2]; //it replaces key[2] with _* (1,2,3)
//then this will follow
$pos = strpos($parts[1], '_'); // this will remove all _* from key [1] if they exist
if($pos !== false) $parts[1] = substr($parts[1], 0, $pos); // tested and works
//echo "<pre>"; print_r($parts); echo "</pre>";
//need code to filter the arrays defined by key [1] via key [2] here?
So for example if I have multiple arrays after my piece of code like this:
Array
(
[0] => A
[1] => 100
[2] => 1
[3] => 1184
[4] => 0
)
Array
(
[0] => A
[1] => 100
[2] => 2
[3] => 1185
[4] => 0
)
Array
(
[0] => A
[1] => 100
[2] => 3
[3] => 1186
[4] => 0
)
Array
(
[0] => A
[1] => 101
[2] => 1
[3] => 1187
[4] => 0
)
Array
(
[0] => A
[1] => 101
[2] => 2
[3] => 1188
[4] => 0
)
Array
(
[0] => A
[1] => 302
[2] => 0
[3] => 1161
[4] => 0
)
After some code to filter the arrays, the final example result will be:
Array
(
[0] => A
[1] => 100
[2] => 3
[3] => 1186
[4] => 0
)
Array
(
[0] => A
[1] => 101
[2] => 2
[3] => 1188
[4] => 0
)
Array
(
[0] => A
[1] => 302
[2] => 0
[3] => 1161
[4] => 0
)
Please I could do with some help on this it only needs a couple of lines, I'm not a programmer but I'd like to finish this project.
Try this :
$array = array("A", "100_1", 0, 1184, 0);
$array = array_map(
function($str) {
return preg_replace_callback('/([\d]+)\_([1-3])/', function($matches){ return $matches[1] + $matches[2]-1;}, $str);
},
$array
);
print_r($array)
Try this:
$part = array();
foreach ($parts as $key => $value) {
if (isset($part[$value[1]])) {
if ($parts[$part[$value[1]]][0][2] < $value[0][2]) {
$part[$value[1]] = $key;
}
} else {
$part[$value[1]] = $key;
//echo "<pre>"; print_r($part); echo "</pre>";
}
}
I change the answer because it was not what I'm looking for but this is.
Consider the array below:
//$allmembers
Array
(
[0] => Array
(
[id] => 7
[name] => John Smith
)
[1] => Array
(
[id] => 8
[name] => John Skeet
)
[2] => Array
(
[id] => 9
[name] => Chuck Norris
)
[3] => Array
(
[id] => 10
[name] => Bruce Lee
)
)
I have another array like this:
//$schedules
Array
(
[0] => Array
(
[id] => 24
[title] => DAMN DAMN DAMN!
[description] =>
[room] => 5022
[start] => 1362783300
[end] => 1362783300
[participants] => 7,8
[members] => Array
(
)
)
[1] => Array
(
[id] => 22
[title] => blah blah
[description] =>
[room] => 5022
[start] => 1365024780
[end] => 1365026280
[participants] => 9,10
[members] => Array
(
)
)
)
So I have to read the participants keys in the second array, and then find the name from first array and add it to the member of the second array.
I am trying the code below but I aint got any success so far:
$allmembers = $_DB->Query("SELECT id,name FROM members");
for($i = 0; $i < count($schedules); $i++)
{
$schedules[$i]["members"] = array() ;
$mems = array();
$mems = explode(',', $schedules[$i]["participants"]);
for($j = 0; $j < count($mems); $j++)
{
//How to search the first array?
}
}
given that the two arrays exist above this block as $schedules and $allmembers, the following should work.
foreach($schedules as &$event)
{
$participants = array_flip(explode(',', $event['participants']));
$addThese = array();
foreach($allmembers as $member)
{
if (isset($participants[$member['id']]))
$addThese[] = $member;
}
$event['participants'] = $addThese;
} unset($event);
print_r($schedules);
I feel like I've done something very silly, but I can't work out what it is.
I'm running through an array of arrays generated by fgetcsv(). What I want is to split the array in two at the spot where the value in one line differs from the one in the next.
This code results in $first holding all but one of the arrays, and $second holding the last - completely ignoring the string comparison. strcmp() results in the same thing.
$csv = array();
$file = fopen('A.csv', 'r');
while (($result = fgetcsv($file)) !== false)
{
$csv[] = $result;
}
fclose($file);
array_shift($csv); //gets rid of the CSV headers
$rows = count($csv);
for ($i = 0; $i <= $rows; $i++){
if ($csv[$i][1] != $csv[$i+1][1]){
$first = array_slice($csv, 0, $i);
$second = array_slice($csv, $i);
}
}
Here's an example of the CSV file:
NAME,MATCHNAME,CHROMOSOME,START LOCATION,END LOCATION,CENTIMORGANS,MATCHING SNPS
A,person_one,2,20945970,23287731,2.48,500
A,person_one,2,233444593,234432885,1.56,500
A,person_one,4,99184637,100861136,1.24,500
A,person_two,1,154990798,157871980,2.8,700 //Here's where the new array should start
A,person_two,1,67136078,70785393,2.28,800
EDIT: My expected $first for this example would be:
Array
(
[0] => Array
(
[0] => A
[1] => person_one
[2] => 2
[3] => 20945970
[4] => 23287731
[5] => 2.48
[6] => 500
)
[1] => Array
(
[0] => A
[1] => person_one
[2] => 2
[3] => 233444593
[4] => 234432885
[5] => 1.56
[6] => 500
)
[2] => Array
(
[0] => A
[1] => person_one
[2] => 4
[3] => 99184637
[4] => 100861136
[5] => 1.24
[6] => 500
)
)
And my expected $second would be:
Array
(
[0] => Array
(
[0] => A
[1] => person_two
[2] => 1
[3] => 154990798
[4] => 157871980
[5] => 2.8
[6] => 700
)
[1] => Array
(
[0] => A
[1] => person_two
[2] => 1
[3] => 67136078
[4] => 70785393
[5] => 2.28
[6] => 800
)
)
It is doing what you want, but when it gets to the last iteration, it compares whatever's in the last line with null, so it overwrites $first and $second.
Try adding break; after the assignments to break out of the loop when the condition is met.