PHP remove undefined offset: - php

I would like to ask what is this kind of error undefined offset:4
my code is
$url = 'http://gogo.com, http://yoyo.com, http://gogo.com, http://yoyo.com, http://gogo.com, http://yoyo.com';
$key = 'key1, key2, key3';
$xurl = explode( "\n", $url );
$xkey = explode( "\n", $key );
$count = count( $xkey );
echo $count;
$i = 0;
while ( $i <= $count ) {
if(empty($xkey[$i])){
unset($xkey[$i]);
}
echo $xkey[$i];
$i++;
}
the echo is key1 key2 key3
but the thing is that i need to loop the xkey equal to my url
so the echo should be but i only have 3keyword i mean the keyword is less than the url.
how can i make it something like this below...
http://gogo.com - key1
http://yoyo.com - key2
http://gogo.com - key3
http://yoyo.com - key1
http://gogo.com - key2
http://yoyo.com - key3

What it means is that the script is looking for the value of $xkey[4], but that element doesn't exist. This is happening because array keys like this are 0-based, so the fourth element will be $xkey[3]. Change your while statement to while ( $i < $count ) as count will be 4, but the max key will be 3.

You're doing
while ( $i <= $count ) {
where $count is the number of element in $xkey (let say 4 elements)
As arrays are 0 indexed, the element $xkey[3] is the 4th and last element.
$xkey[4] will bring you that error.
Now, delete "=" in this while ( $i <= $count ) { and it should disappear.

I'm not sure where to start explaining what your problem is, you have an entirely wrong approach. To get the result you want you need to do this:
$urlString = 'http://gogo.com, http://yoyo.com, http://gogo.com, http://yoyo.com, http://gogo.com, http://yoyo.com';
$keyString = 'key1, key2, key3';
$urls = explode(',', $urlString);
$keys = explode(',', $keyString);
$i = 0;
$count = count($keys);
foreach ($urls as $url) {
echo $url, ' - ', $keys[$i % $count], PHP_EOL;
$i++;
}

Related

How to split evenly and oddly a string to form an array of even and odd results OK Like

I have a php string formed by images and corresponding prices like OK Like
$myString = "ddb94-b_mgr3043.jpg,3800,83acc-b_mgr3059.jpg,4100";
I know that if I do:
$myArray = explode(',', $myString);
print_r($myArray);
I will get :
Array
(
[0] => ddb94-b_mgr3043.jpg
[1] => 3800
[2] => 83acc-b_mgr3059.jpg
[3] => 4100
)
But How could I split the string so I can have an associative array of the form?
Array
(
"ddb94-b_mgr3043.jpg" => "3800"
"83acc-b_mgr3059.jpg" => "4100"
)
Easier way to do like below:-
<?php
$myString = "ddb94-b_mgr3043.jpg,3800,83acc-b_mgr3059.jpg,4100";
$chunks = array_chunk(explode(',', $myString), 2); //chunk array into 2-2 combination
$final_array = array();
foreach($chunks as $chunk){ //iterate over array
$final_array[trim($chunk[0])] = trim($chunk[1]);//make key value pair
}
print_r($final_array); //print final array
Output:-https://eval.in/859757
Here is another approach to achieve this,
$myString = "ddb94-b_mgr3043.jpg,3800,83acc-b_mgr3059.jpg,4100,test.jpg,12321";
$arr = explode(",",$myString);
$temp = [];
array_walk($arr, function($item,$i) use (&$temp,$arr){
if($i % 2 != 0) // checking for odd values
$temp[$arr[$i-1]] = $item; // key will be even values
});
print_r($temp);
array_walk - Apply a user supplied function to every member of an array
Here is your working demo.
Try this Code... If you will receive all the key and value is equal it will work...
$myString = "ddb94-b_mgr3043.jpg,3800,83acc-b_mgr3059.jpg,4100";
$myArray = explode(',', $myString);
$how_many = count($myArray)/2;
for($i = 0; $i <= $how_many; $i = $i + 2){
$key = $myArray[$i];
$value = $myArray[$i+1];
// store it here
$arra[$key] = $value;
}
print_r($arra);

Reset Duplicate Values in PHP Array

I have a PHP array:
$arr = array(1,2,3,3,4,6,6);
I want to find the location of either duplicate in each duplicate pair (either 3 and either 6) and reset that value using rand(1,8). How would I go about doing this? I essentially need to make sure all of the array values are different.
You can try
$arr = array(1,2,3,3,4,6,6);
$dup = array_diff_assoc($arr,array_unique($arr));
$v = mt_rand(1, 8);
foreach ( $dup as $k ) {
while ( in_array($v, $arr) ) {
$v = mt_rand(1, 8);
}
$arr[$k] = $v;
}
echo "<pre>";
print_r($arr);
A simple way is to record how many items are in the array, use array_unique, and finally refill the array using a rand:
$size = count($arr);
$arr = array_unique($arr);
while (count($arr) < $size) {
$arr[] = rand(1,8,$arr);
}
You'd want to repeat this until count($arr) == count(array_unique($arr)). You could also make a random function that gives random values that are not already in the array pretty easily using in_array() and a loop.

concatenate two index values in for each

I have a loop like this
$rr=array();
foreach($relations as $key=>$type){
$rr[$relationType->U2U_Related_USR_ID]=$type[$k]->MSTT_Name.' / '.$type[$k+1]->MSTT_Name;
$k++;
}
Am getting only first index value. how to concatenate two index values in for each.
Increment by 2 !
$rr = array();
for ($i = 0, $n = count($type); $i < $n; $i += 2) {
$t1 = $type[$i];
$t2 = $type[$i + 1];
$rr[$relationType->U2U_Related_USR_ID] = $t1->MSTT_Name.' / '.$t2->MSTT_Name;
}
Note: $type's length should be an even number!
you can work with 2 couples key/value inside your loop like this :
foreach($relations as $key=>$type){
list( $odd_key, $odd_value ) = each( $relations );
//... your code here
// This work with a step by 2 elements. If you need step by 1,
// add the following line at the end of the loop :
//prev( $relations )
}

How to count order number of values in an array?

I have an array with a few values and want to do something like this:
$arrayvalues = array_reverse(explode(', ', somefunction()));
foreach ( $arrayvalues as $arrayvalue ) :
printf('<li class="'.$countvalue.'">'.$arrayvalue.'</li>');
endforeach;
I want to have in $countvalue the number of the value in the array
ie... the array will be something like this: ("apple", "orange", "grapefruit")
I want the number to match the order number of these values
apple = 1, orange = 2, grapefruit = 3
or actually even if it's just an incremental number according to the values echoed it doesn't matter, I just need to insert a css class represented by an incremembtal number
I tried playing $i... count... but I don't know how to achieve what I want; I'm more a designer than a coder, I looked in the PHP help but couldn't find a clear solution for my case
thank you
You already have an incremental number based on order. Keep in mind, this only works if your key's are 0-based. If you use an associative array you will need to use a for loop instead (as suggested by nickb).
$arrayvalues = array_reverse(explode(', ', somefunction()));
foreach ( $arrayvalues as $key => $arrayvalue ){
echo "<li class='$key'>$arrayvalue</li>";
}
$arrayvalues = array_reverse(explode(', ', somefunction()));
$i = 0;
foreach ( $arrayvalues as $arrayvalue )
{
$i++;
printf('<li class="'.$i.'">'.$arrayvalue.'</li>');
}
Use a for loop to iterate over your array, like so:
for( $i = 0, $j = count( $arrayvalues); $i < $j; $i++) :
printf('<li class="' . ($i + 1) . '">' . $arrayvalues[$i] . '</li>');
endfor;
If you want the index $i to start at one, you need to add one in the printf statement.
Side note: You don't need printf here if you're not actually generating formatted output.
$arrayvalues = array_reverse(explode(', ', somefunction()));
$i=0;
foreach ( $arrayvalues as $arrayvalue ) :
++$i;
$countvalue = $i;
printf('<li class="'.$countvalue.'">'.$arrayvalue.'</li>');
endforeach;
We (or I) advise you use a normal for loop.
for($i = 0; $i < count($arrayvalues); $i++) {
printf('<li class="'.($i+1).'">'.$arrayvalue.'</li>');
}

php basic operations question

I have a array of say 50 elements. This array can be of size anything.
I want to have the first 10 elements of the array in a string.
I have the program as:
$array1= array("itself", "aith","Inside","Engineer","cooool","that","it","because");
$i=0;
for($f=0; $f < sizeof(array1); $f++)
{
$temparry = $temparry.array1[$f];
if(($f%10) == 0 && ($f !== 0))
{
$temparray[$i] = $temparray;
$i++;
}
}
==
so that at the end:
I get
temparray1= first 10 elements
temparray2 - next 10 elemnts...
I am not what I am missing in my loops.
After reading your comment, I think you want array_chunk [docs]:
$chunks = array_chunk($array1, 10);
This will create a multidimensional array with each element being an array containing 10 elements.
If you still want to join them to a string, you can use array_map [docs] and implode [docs]:
$strings = array_map('implode', $chunks);
This gives you an array of strings, where each element is the concatenation of a chunk.
This is something you can easily do with array_splice and implode.
Example:
<?php
$array = range(1, 50);
while ( $extracted = array_splice($array, 0, 10) )
{
// You could also assign this to a variable instead of outputting it.
echo implode(' ', $extracted);
}
all you are doing here is creating a temporary value and then deleting it. To save it into a string:
$myArray = array("itself", "aith","Inside","Engineer",
"cooool","that","it","because");
$myString = '';
for($i = 0; $i < 10; $i++) {
$myString .= $myArray[$i];
}
You could also run that inside of another for loop that would run through the entire array giving you ten-element increments.
Actually you can use arrray_slice and implode functions like this:
// put first 10 elements into array output
$output = array_slice($myArray, 10);
// implode the 10 elements into a string
$str = implode("", $output);
OP's fixed code as per comments below:
$array1= array("itself","aith","Inside","Engineer","cooool","that","it","because");
$temparry='';
$temparray = array();
for($f=0; $f < count($array1); $f++)
{
$temparry = $temparry.$array1[$f];
if(($f%3) == 0 && ($f !== 0))
{
$temparray[] = $temparry;
$temparry = '';
}
}
print_r($temparray);

Categories