English 中文(简体)
斜体阵列——我需要分一半的阵列
原标题:Cannot slice array - I need to split an array in half

我的阵列如下。 我想把它搁置一半,从两个半部分中创造两个新的阵列。 然而,如果我使用阵列——slice,它就永远不会恢复。 我不理解为什么。

array
   pic  => 
    array
      0 => string  740  (length=3)
      1 => string  741  (length=3)
      2 => string  742  (length=3)
      3 => string  748  (length=3)
   alt  => 
    array
      0 => string    (length=0)
      1 => string    (length=0)
      2 => string  Test caption 1  (length=14)
      3 => string  Test caption 2  (length=14)

我需要的阵列应该保留钥匙,只有一半被搁置,因此,我有两个补充部分。 例如,上半部分应当考虑:

array
   pic  => 
    array
      0 => string  740  (length=3)
      1 => string  741  (length=3)
   alt  => 
    array
      0 => string    (length=0)
      1 => string    (length=0)

和后半期:

array
   pic  => 
    array
      0 => string  742  (length=3)
      1 => string  748  (length=3)
   alt  => 
    array
      0 => string  Test caption 1  (length=14)
      1 => string  Test caption 2  (length=14)

thanks

页: 1

$lodgepics = get_field( accommodation_rooms );
$featuredpics = get_field( featured_pics );
$showcasepics = array();
foreach ($featuredpics as $featuredpic) {
    if (isset($featuredpic[ featured_pic ])&&!empty($featuredpic[ featured_pic ])) $showcasepics[pic][] = $featuredpic[ featured_pic ];
    if (isset($featuredpic[ featured_alt ])) $showcasepics[alt][] = $featuredpic[ featured_alt ];
else $showcasepics[alt][] =   ;
}
foreach ($lodgepics as $lodgepic) {
    if(isset($lodgepic[ accommodation_roomphoto ])&&!empty($lodgepic[ accommodation_roomphoto ])) $showcasepics[pic][] = $lodgepic[ accommodation_roomphoto ];
    if(isset($lodgepic[ accommodation_roomname ])&&!empty($lodgepic[ accommodation_roomname ])) $showcasepics[alt][] = $lodgepic[ accommodation_roomname ];
    else $showcasepics[alt][] =   ;
}
最佳回答

Hard for me to grasp the need to do something like what you are proposing, if it were me I wouldn t populate an array using your structure, I would create an array like this:

array
  0 => 
    array
       pic  => string  740  (length=3)
       alt  => string    (length=0)
  1 => 
    array
       pic  => string  741  (length=3)
       alt  => string    (length=0)
  2 => 
    array
       pic  => string  742  (length=3)
       alt  => string  Test Caption 1  (length=0)
  3 => 
    array
       pic  => string  748  (length=3)
       alt  => string  Test Caption 2  (length=0)

This array structure keeps the image attributes together, rather than keeping them separate.

tomtheman5 has 80% there, but left out the offset, and length params to the array_slice calls that need to be passed to get the correct slices. However he does make a good point, if you stick with your array structure, you may run into issues if you do not have matching elements in each pic and alt arrays. If this is not a concern, then consider the following snippet:

$slice1 = array();
$slice2 = array();    

foreach ($array as $key => $value) {

    $arrayCount = count($array[$key]);
    $arrayHalfCount = ($arrayCount / 2);

    $slice1[$key] = array_slice($array[$key], 0, $arrayHalfCount);
    $slice2[$key] = array_slice($array[$key], $arrayHalfCount);
}

--——————

$lodgepics = get_field( accommodation_rooms );
$featuredpics = get_field( featured_pics );
$showcasepics = array();
foreach ($featuredpics as $featuredpic) {
    if ((isset($featuredpic[ featured_pic ])) && (!empty($featuredpic[ featured_pic ]))) {
        $currentPic[ pic ] = $featuredpic[ featured_pic ];
        $currentPic[ alt ] = (isset($featuredpic[ featured_alt ])) ? $featuredpic[ featured_alt ] :   ; 
        $showcasepics[] = $currentPic;
}
}

foreach ($lodgepics as $lodgepic) {
    if ((isset($lodgepic[ accommodation_roomphoto ])) && (!empty($lodgepic[ accommodation_roomphoto ]))) {
        $currentPic[ pic ] = $lodgepic[ accommodation_roomphoto ];    
        $currentPic[ alt ] = (isset($lodgepic[ accommodation_roomname ])) ? $lodgepic[ accommodation_roomname ] :   ;
        $showcasepics[] = $currentPic;
}
}

$showcasepicsCount = count($showcasepics);
$showcasepicsHalfCount = ($showcasepicsCount / 2);

$slice1 = array_slice($showcasepics, 0, $showcasepicsHalfCount);
$slice2 = array_slice($showcasepics, $showcasepicsHalfCount);
问题回答

这些是实际阵列。 主要的阵列是一个联系阵列,因此,为了获得你的许可,你需要穿透阵列的所有钥匙:

$slice1 = array();
$slice2 = array();
foreach ($array as $key => $value)
{
    $slice1[$key] = array_slice($array[$key], ...);
    $slice2[$key] = array_slice($array[$key], ...);
}

阵列——把一个阵列铺到丘库

$array1 = array("1" => "green", "2" => "brown", "3" => "blue", "4" => "black");
$array2 = array("5" => "yellow","6" => "orange","7"=> "violet","8"=> "white");

$arraylength1   = ceil(count($array1)/2);
$res1           = array_chunk($array1,$arraylength1);
$arraylength2   = ceil(count($array2)/2);
$res2           = array_chunk($array2,$arraylength2);

echo "<pre>";
print_r($res1);
print_r($res2);
echo "</pre>";
exit;

Try This.





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签