在数组中找到多个结果并回显它们

时间:2018-11-22 09:42:42

标签: php arrays

我遇到了问题,但是似乎找不到任何答案。

我有一个数组示例:

$data = array(
    '8006309' => 'Pallet 1',
    '8006309' => 'Pallet 2',
    '8006309' => 'Pallet 2',
    '8006309' => 'Pallet 3',
    '8016493' => 'Pallet 4',
    '8014376' => 'Pallet 5',
    '8014375' => 'Pallet 5',
    '8014375' => 'Pallet 5',
    '8006401' => 'Pallet 6',
    '8006310' => 'Pallet 6',
    '8004263' => 'Pallet 6',
    '8001038' => 'Pallet 6',
    '8000697' => 'Pallet 6',
    '004-9866' => 'Pallet 6'
);

您会看到有些行具有相同的数据,例如:8006309。我想在搜索8006309时显示结果,例如:

Pallet 1
Pallet 2
Pallet 2
Pallet 3

当前,当我搜索结果时,我只会得到显示的最后一个值。

我不是那里最好的程序员,所以这个问题对于你们中的某些人可能是小菜一碟。尽管我还不太清楚该怎么做?

任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:0)

您将同一数组键分配给多个值。 “ 8006309”将被覆盖,直到到达最后一个。

如果要为每个键(例如8006309)存储这些托盘字符串,则可能应该使用多维数组。

import random
import math
import numpy as np
import matplotlib.pyplot as plt

color_list = [ 'b', 'g', 'r', 'c', 'y', 'm' ]

def histogram_and_curves( array, mean = 0.0, stdDev = 1.0, bins = None, xAxis = 'X', yAxis = 'Y', zAxis = 'Z', show = True, *curves ):

"""
Plots a histogram of a data array in 1 or 2 dimensions and an arbitrary number of PDFs for comparison.
"""

color = 'k'
bgcolor = 'w'
style = 'step'

fig = plt.figure( figsize = (6,6) )

if array.ndim is 1:

    ax = fig.add_subplot( 111, facecolor = bgcolor )

    if bins is None:
        bins = np.arange( math.floor( np.amin( array ) ), math.ceil( np.amax( array ) ), 0.01 )

    XMIN = mean - ( 4 * stdDev )
    XMAX = mean + ( 4 * stdDev )

    t = np.arange( XMIN , XMAX, 0.01)

    xlim = ax.set_xlim( XMIN, XMAX )
    ylim = ax.set_ylim( 0, 1 )

    xText = ax.set_xlabel( xAxis )
    yText = ax.set_ylabel( yAxis )

    # Plot the 1D histogram
    n, bins, patches = ax.hist( array, bins = bins, density = True, color = color, histtype = style )

    # Plot distribution curves
    if curves:
        for curve in curves:
            ax.plot( t, curve, color = random.choice( color_list ) )

    if show:
        plt.show()
    else:
        plt.close()

elif array.ndim is 2:
    raise ValueError( "I'll come back for you..." )

else:
    print( "Invalid dimensions. Required: 1 or 2. (Actual: {})".format( array.ndim ) )

return ax

因此,您只有一个钥匙分配给多个托盘。 您还可以在第二个数组中包含键,例如:

    $data = array(
                  '8006309' => array('Pallet 1', 'Pallet 2', ...),
                  '8004773' => array(...), 
                  ...);

答案 1 :(得分:0)

您应该将您的数组元素定义为另一个数组,这是一个测试:

$data = array(
    '8006309' => array('Pallet 1','Pallet 2', 'Pallet 3'),
    '8016493' => 'Pallet 4',
    '8014376' => 'Pallet 5',
    '8014375' => 'Pallet 5',
    '8014375' => 'Pallet 5',
    '8006401' => 'Pallet 6',
    '8006310' => 'Pallet 6',
    '8004263' => 'Pallet 6',
    '8001038' => 'Pallet 6',
    '8000697' => 'Pallet 6',
    '004-9866' => 'Pallet 6'
);