PHP嵌套数组获取特定键

时间:2017-02-22 08:24:32

标签: php

<?php

namespace AppBundle\Entity;

use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\ORM\Mapping as ORM;

/**
 * Job
 *
 * @ApiResource
 * @ORM\Entity
 */
class Job
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="title", type="string", length=100, nullable=false)
     */
    private $title;

    /**
     * @var string
     *
     * @ORM\Column(name="description", type="text")
     */
    private $description;

    /**
     * @var Location The location this job is about.
     *
     * @ORM\ManyToOne(targetEntity="Location", inversedBy="jobs")
     */
    private $location;

尝试从每个阵列获得“totalprice”。

Array ( [0] => Array ( [totalprice] => 671 [total] => 0 [price_per_p] => 2 ) [1] => Array ( [totalprice] => 312 [total] => 0 [price_per_p] => 2 ) )

但这根本不起作用。

$i = 0;
foreach($arr as $x_values => $x) {
   echo $x[$i]["totalprice"];
   $i++;
}

我想首先从数组1获取“循环”,然后在第二个“循环”获取数组2

1 个答案:

答案 0 :(得分:0)

只需删除$ i索引即可。不需要$ i变量。

public abstract class PageablePanelAbstract<T extends NamedWithMimeType & Serializable> extends ScalarPanelAbstract {

    private static final long serialVersionUID = 1L;
    @SuppressWarnings("unused")
    private static final Logger LOG = LoggerFactory.getLogger(PageablePanelAbstract.class);
    private static final String ID_SCALAR_IF_REGULAR = "scalarIfRegular";
    private static final String ID_SCALAR_IF_REGULAR_CLEAR = "scalarIfRegularClear";
    private static final String ID_SCALAR_NAME = "scalarName";
    private static final String ID_SCALAR_VALUE = "scalarValue";
    private static final String ID_SCALAR_IF_COMPACT = "scalarIfCompact";

    private static final String PAGE_SIZE = "Page Size";
    private static final String PAGE_NUMBER = "Page Number";
    private TextField<T> textField;
    private TextField<T> textField2;

    protected WebMarkupContainer scalarTypeContainer;

    public PageablePanelAbstract(String id, ScalarModel scalarModel) {
        super(id, scalarModel);
    }

    @Override
    protected void onInitialize() {
        super.onInitialize();

        scalarTypeContainer = new WebMarkupContainer(ID_SCALAR_IF_REGULAR);
        scalarTypeContainer.add(new CssClassAppender(getScalarPanelType()));
        addOrReplace(scalarTypeContainer);
    }

    @Override
    protected FormGroup addComponentForRegular() {

        textField = createTextField(ID_SCALAR_VALUE);
        textField.setLabel(Model.of(getModel().getName()));
        textField.setRequired(getModel().isRequired());

        textField2 = createTextField("scalarValue1");
        textField2.setLabel(Model.of(getModel().getName()));
        textField2.setRequired(getModel().isRequired());

        final FormGroup labelIfRegularTextField = new FormGroup(ID_SCALAR_IF_REGULAR, textField);
        labelIfRegularTextField.add(textField);
        labelIfRegularTextField.add(textField2);

        final Label scalarNameTextField = new Label(ID_SCALAR_NAME, PAGE_NUMBER);
        labelIfRegularTextField.add(scalarNameTextField);

        final Label scalarNameTextField1 = new Label("scalarName1", PAGE_SIZE);
        labelIfRegularTextField.add(scalarNameTextField1);

        // find the links...
        final List<LinkAndLabel> entityActions = EntityActionUtil.getEntityActionLinksForAssociation(this.scalarModel, getDeploymentCategory());

        addPositioningCssTo(labelIfRegularTextField, entityActions);

        addOrReplace(labelIfRegularTextField);
        addFeedbackOnlyTo(labelIfRegularTextField, textField);
        addEditPropertyTo(labelIfRegularTextField);

        // ... add entity links to panel (below and to right)
        addEntityActionLinksBelowAndRight(labelIfRegularTextField, entityActions);

        return labelIfRegularTextField;
    }

    @Override
    protected Component addComponentForCompact() {
        final MarkupContainer scalarIfCompact = new WebMarkupContainer(ID_SCALAR_IF_COMPACT);
        addOrReplace(scalarIfCompact);
        return scalarIfCompact;
    }

    @Override
    protected void addFormComponentBehavior(Behavior behavior) {
        textField.add(behavior);
    }

    protected void onBeforeRenderWhenViewMode() {
        updateRegularFormComponents(InputFieldVisibility.NOT_VISIBLE);
    }

    protected void onBeforeRenderWhenDisabled(final String disableReason) {
        updateRegularFormComponents(InputFieldVisibility.NOT_VISIBLE);
    }

    protected void onBeforeRenderWhenEnabled() {
        updateRegularFormComponents(InputFieldVisibility.VISIBLE);
    }

    @SuppressWarnings("unchecked")
    private T getText(final ScalarModel model) {
        ObjectAdapter adapter = model.getObject();
        return adapter != null ? (T) adapter.getObject() : null;
    }

    private void updateRegularFormComponents(final InputFieldVisibility visibility) {
        MarkupContainer formComponent = (MarkupContainer) getComponentForRegular();
        formComponent.get(ID_SCALAR_VALUE).setVisible(visibility == InputFieldVisibility.VISIBLE);

        updateClearLink(visibility);

    }

    private void updateClearLink(InputFieldVisibility visibility) {
        final MarkupContainer formComponent = (MarkupContainer) getComponentForRegular();
        formComponent.setOutputMarkupId(true); // enable ajax link

        final AjaxLink<Void> ajaxLink = new AjaxLink<Void>(ID_SCALAR_IF_REGULAR_CLEAR) {
            private static final long serialVersionUID = 1L;

            @Override
            public void onClick(AjaxRequestTarget target) {
                setEnabled(false);
                ScalarModel model = PageablePanelAbstract.this.getModel();
                model.setObject(null);
                target.add(formComponent);
                // target.add(fileNameLabel);
            }
        };
        ajaxLink.setOutputMarkupId(true);
        formComponent.addOrReplace(ajaxLink);

        final T txt = getTextFromModel();
        formComponent.get(ID_SCALAR_IF_REGULAR_CLEAR).setVisible(txt != null && visibility == InputFieldVisibility.VISIBLE);
    }

    private T getTextFromModel() {
        return getText(getModel());
    }

    protected enum InputFieldVisibility {
        VISIBLE, NOT_VISIBLE
    }

    protected TextField<T> createTextField(final String componentId) {

        return new TextField<>(componentId, newTextFieldValueModel());
        // return (TextField) textField;
    }

    protected TextFieldValueModel<T> newTextFieldValueModel() {
        return new TextFieldValueModel<>(this);
    }

    protected abstract IModel<String> getScalarPanelType();
}